#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. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 1. pair each element of a list with a given element (define (pair-with e 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) (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) (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) (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) (check-expect (unzip '(a b c d e)) '(a c e)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 6. drop the first element of a list (define (drop-first L) (check-expect (drop-first '(a b c)) '(b c)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 7. drop the element at index N (define (drop N 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 e N 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 index N with e (define (replace e N 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 (drop-all 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) ; 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)