;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; U. C. Berkeley ;; ;; EECS Computer Science Division ;; ;; CS3 Lecture 10 (Lists) Answers ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Code to make lecture faster ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define an-illegal-sentence '(cs3 is great (but that is my (dans) opinion) and fun)) (define another-illegal-sentence '(#f #t #t)) '(yo whassup GoBears CS3isNumber 1) (+ 3 5) (+ 1 (/ 4 2) (+ (- 5 1) (remainder (truncate 4.9) 3))) (define simple '(1 2 3 4) ) simple (define complex '(a (b c) () ((d)) ) ) complex (define meals '((breakfast (3 rasberry poptarts) (7 eggs) (5 mango smoothies)) (lunch (2 advil) (3 burritos) (4 yoo-hoos)) (dinner (3 pepto-bismol) (4 advil) (2 nyquil) (1 pearl milk tea)))) '() '(this is a list) (cons 'cs3 '()) (cons 'love (cons 'cs3 '())) (cons 'i (cons 'love (cons 'cs3 '()))) (cons '(1 2) '(3 4)) (list 1 2 5 'three-sir 3) (list 1 2 5 '(3 sir) 3) (append '(1 2) '(3 4)) (append '(1) '(2 (3)) '() '(4 5 6) '(((7))) ) (define my-list '(cs3 is great (but that is my (dans) opinion) and fun)) my-list (car my-list) (cdr my-list) (cdr (cdr my-list)) (cdr (cdr (cdr my-list))) (cdr (cdr (cdr (cdr my-list)))) (car '()) (cdr '()) my-list (car (cdr (cdr (cdr my-list)))) (cadddr my-list) (define (are-you-a-list-hof l) (map list? l)) (are-you-a-list-hof '(a (b c) () ((d))) ) (filter list? '(a (b c) () ((d))) ) (filter word? '(a (b c) () ((d))) ) (map list '(a b c d)) (reduce append '((a) (b) (c) (d)) ) (length '(1 2 3 4) ) (length '(a (b c) () ((d))) ) (length '()) (null? '(1 2 3 4) ) (null? '() ) (list? '(1 2 3 4) ) (list? '() ) (list? 'cs3 ) (list-ref '(a b c d) 1) (list-ref '(a b c d) 5) (list-ref '(a (b c) () ((d))) 3) (list-ref '() 0) (equal? '(a (b c) () ((d))) '(a (b c) () ((d))) ) (member '(b c) '(a (b c) () ((d))) ) (member 'a-new-word '(a (b c) () ((d))) ) (deep-add-1 '(1 (2 3) () ((4))) ) ;; ==> (2 (3 4) () ((5))) (define (deep-add-1 L) (map (lambda (elt) (if (list? elt) (deep-add-1 elt) (+ 1 elt))) L)) (deep-add-1 '(1 (2 3) () ((4))) )