#lang racket ; List Kata (require htdp/testing) ; Construct functions that do the following. ; Use high-order functional programming if possible, ; otherwise use low-order functional programming. ; These are various solutions discussed in class on Monday 10/13/2010. ; In some cases, the preferred solution is commented out. In others, the ; non-preferred solution is commented out. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 1. pair each element of a list with a given element (define (pair-with e L) (if (null? L) '() (cons (list e (first L)) (pair-with e (rest L))))) ; (map (lambda(y) (list e y)) L)) (check-expect (pair-with 'x '(1 2 3)) '((x 1) (x 2) (x 3))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 2. insert an element at the end of a list (define (snoc e L) ; (append L (list e))) (if (null? L) (list e) (cons (first L) (snoc e (rest L))))) (check-expect (snoc 'x '(1 2 3)) '(1 2 3 x)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 3. insert an element between each element of a list (define (insert-between-each e L) (if (or (null? L) (null? (rest L))) L ; (append (list (first L) e) (insert-between-each e (rest L))))) (cons (first L) (cons e (insert-between-each e (rest L)))))) (check-expect (insert-between-each 'x '(1 2 3)) '(1 x 2 x 3)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 4. zip (interleave) two lists that are known to be the same length (define (zip L M) ; (map list L M)) (if (null? L) '() (cons (list (first L) (first M)) (zip (rest L) (rest M))))) (check-expect (zip '(a b c) '(1 2 3)) '((a 1) (b 2) (c 3))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 5. unizp a list into a list of its first, third, fifth, ... elements (define (unzip L) (if (or (null? L) (null? (rest L))) L (cons (first L) (unzip (rest (rest L)))))) (check-expect (unzip '(a b c d e)) '(a c e)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 6. drop the first element of a list (define (drop-first L) (if (null? L) (error "Don't this: drop-first on empty list") (rest L))) (check-expect (drop-first '(a b c)) '(b c)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 7. drop the element at index N (define (drop N L) (if (null? L) L (if (equal? N 0) (rest L) (cons (first L) (drop (- N 1) (rest L)))))) (check-expect (drop 0 '(a b c)) '(b c)) (check-expect (drop 1 '(a b c)) '(a c)) (check-expect (drop 2 '(a b c)) '(a b)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 8. insert an element at index N ;(define (insert-helper e N L Acc) ; (if (equal? N Acc) ; (cons e L) ; (cons (first L) (insert-helper e N (rest L) (+ 1 Acc))))) (define (insert e N L) ;(insert-helper e N L 0)) (if (equal? N 0) (cons e L) (cons (first L) (insert e (- N 1) (rest L))))) (check-expect (insert 'x 0 '(a b c)) '(x a b c)) (check-expect (insert 'x 1 '(a b c)) '(a x b c)) (check-expect (insert 'x 2 '(a b c)) '(a b x c)) (check-expect (insert 'x 3 '(a b c)) '(a b c x)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 9. replace the element at index N with e (define (replace e N L) (cond ((null? L) (error "in replace")) ((equal? N 0) (cons e (rest L))) (else (cons (first L) (replace e (- N 1) (rest L)))))) ; ; (if (null? L) ; (error "in replace") ; (if (equal? N 0) ; (cons e (rest L)) ; (cons (first L) (replace e (- N 1) (rest L)))))) (check-expect (replace 'x 0 '(a b c)) '(x b c)) (check-expect (replace 'x 1 '(a b c)) '(a x c)) (check-expect (replace 'x 2 '(a b c)) '(a b x)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 10. drop all occurrences of e from a list ;(define (bool-func a) ; (if (equal? a e) ; #t ; #f)) (define (drop-all e L) (filter (lambda(x) (not (equal? x e))) L)) (check-expect (drop-all 'x '(a x b x x c x)) '(a b c)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 11. For F being a function that produces a list from any element of a list, ; append together the results of applying f to each element (define (mappend f L) (foldr append '() (map f L))) ; functions for illustration purposes (define (range M N) (if (> M N) '() (cons M (range (+ 1 M) N)))) (check-expect (range 1 5) '(1 2 3 4 5)) (define (from1 N) (range 1 N)) (check-expect (from1 5) '(1 2 3 4 5)) (check-expect (mappend from1 '(1 2 3 4)) '(1 1 2 1 2 3 1 2 3 4)) (generate-report)