Created
July 14, 2010 09:15
-
-
Save cametan001/475215 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; P01 (*) Find the last box of a list. | |
| ;; Example: | |
| ;; * (my-last '(a b c d)) | |
| ;; (D) | |
| (define (my-last lst) | |
| (if (null? lst) | |
| '() | |
| (let ((tail (cdr lst))) | |
| (if (null? tail) | |
| lst | |
| (my-last tail))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; P02 (*) Find the last but one box of a list. | |
| ;; Example: | |
| ;; * (my-but-last '(a b c d)) | |
| ;; (C D) | |
| (define (my-but-last lst) | |
| (let ((revlis (reverse lst))) | |
| (cond | |
| ((null? revlis) '()) | |
| ((< (length revlis) 3) lst) | |
| (else `(,(cadr revlis) ,(car revlis)))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; P03 (*) Find the K'th element of a list. | |
| ;; The first element in the list is number 1. | |
| ;; Example: | |
| ;; * (element-at '(a b c d e) 3) | |
| ;; C | |
| #lang racket | |
| (provide element-at) | |
| (define (element-at lst n) | |
| (list-ref lst (- n 1))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; P04 (*) Find the number of elements of a list. | |
| (define (longitude lst) | |
| (let loop ((lst lst) (acc 0)) | |
| (if (null? lst) | |
| acc | |
| (loop (cdr lst) (+ 1 acc))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; P05 (*) Reverse a list. | |
| (define (reverses lst) | |
| (let loop ((lst lst ) (acc '())) | |
| (if (null? lst) | |
| acc | |
| (loop (cdr lst) (cons (car lst) acc))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; P06 (*) Find out whether a list is a palindrome. | |
| ;; A palindrome can be read forward or backward; e.g. (x a m a x). | |
| (define (palin? lst) | |
| (equal? lst (reverse lst))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; P07 (**) Flatten a nested list structure. | |
| ;; Transform a list, possibly holding lists as elements into a `flat' list by replacing each list with its elements (recursively). | |
| ;; Example: | |
| ;; * (my-flatten '(a (b (c d) e))) | |
| ;; (A B C D E) | |
| ;; Hint: Use the predefined functions list and append. | |
| (define (my-flatten lst) | |
| (if (null? lst) | |
| '() | |
| (let ((head (car lst)) (flat-tail (my-flatten (cdr lst)))) | |
| (append ((if (pair? head) my-flatten list) head) flat-tail)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; P08 (**) Eliminate consecutive duplicates of list elements. | |
| ;; If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be changed. | |
| ;; Example: | |
| ;; * (compress '(a a a a b c c a a d e e e e)) | |
| ;; (A B C A D E) | |
| (define (compress lst) | |
| (if (null? lst) | |
| '() | |
| (let loop ((lst lst) (acc '())) | |
| (let ((head (car lst)) (tail (cdr lst))) | |
| (let ((cha (cons head acc))) | |
| (if (null? tail) | |
| (reverse cha) | |
| (loop tail (if (eq? head (car tail)) acc cha)))))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; P09 (**) Pack consecutive duplicates of list elements into sublists. | |
| ;; If a list contains repeated elements they should be placed in separate sublists. | |
| ;; Example: | |
| ;; * (pack '(a a a a b c c a a d e e e e)) | |
| ;; ((A A A A) (B) (C C) (A A) (D) (E E E E)) | |
| (require srfi/1) | |
| (define (pack lst) | |
| (let loop ((lst lst) (acc '())) | |
| (if (null? lst) | |
| (reverse acc) | |
| (loop (strip lst) (cons (picks lst) acc))))) | |
| (define-syntax define-pack-aux | |
| (syntax-rules () | |
| ((_ name proc) | |
| (define (name lst) | |
| (let ((head (car lst))) | |
| (proc (lambda (x) | |
| (eq? head x)) lst)))))) | |
| ;; Using a procedure, take-while, defined in SRFI-1 | |
| (define-pack-aux picks take-while) | |
| ;; Using a procedure, drop-while, defined in SRFI-1 | |
| (define-pack-aux strip drop-while) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; P10 (*) Run-length encoding of a list. | |
| ;; Use the result of problem P09 to implement the so-called run-length encoding data compression method. Consecutive duplicates of elements are encoded as lists (N E) where N is the number of duplicates of the element E. | |
| ;; Example: | |
| ;; * (encode '(a a a a b c c a a d e e e e)) | |
| ;; ((4 A) (1 B) (2 C) (2 A) (1 D)(4 E)) | |
| #lang racket | |
| (provide encode) | |
| (require srfi/1) | |
| (define (encode lst) | |
| (let loop ((lst lst) (acc '())) | |
| (if (null? lst) | |
| (reverse acc) | |
| (loop (strip lst) | |
| (cons | |
| `(,(length (picks lst)) ,(car lst)) | |
| acc))))) | |
| (define-syntax define-encode-aux | |
| (syntax-rules () | |
| ((_ name proc) | |
| (define (name lst) | |
| (let ((head (car lst))) | |
| (proc (lambda (x) | |
| (eq? head x)) lst)))))) | |
| ;; Using a procedure, take-while, defined in SRFI-1 | |
| (define-encode-aux picks take-while) | |
| ;; Using a procedure, drop-while, defined in SRFI-1 | |
| (define-encode-aux strip drop-while) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment