; Problem 1 (butfirst (butlast (se '(cooler?) 'jeff '(chung)))) (jeff) (first '(word 1 2 3)) word (se (first 3) (bf 3)) (3 "") ((23)) error can't apply 23 ((define (square x) (* x x)) 3) error can't apply 'square ((first 'first) 'butfirst) error can't apply 'f ((lambda (x y) x) '(1 2)) error too few arguments (define (f) (g)) (define (g) f) (f) procedure (same as f) (cond (first 'hello) (first '(1 2 3 4)) (else 19)) hello (define (a b c) (define (b a) a) (b c)) (a 4 2) 2 (every ((lambda (x) (x 3)) +) '(1 2 3 4)) error 3 is not a procedure (1st argument to every) (keep (lambda (x) x) '(false = true)) (false = true) ; Problem 2 (define (running-total s) (cond ((empty? s) '()) ((empty? (bf s)) s) (else (let ((b (running-total (bl s)))) (se b (+ (last b) (last s))))))) ; Problem 3 (define (interleave a b) (if (or (empty? a) (empty? b)) (word a b) (word (first a) (first b) (interleave (bf a) (bf b))))) ; Problem 4 (define (count-change total coinvalue coincount) (cond ((= total 0) 1) ((or (< total 0) (empty? coinvalue)) 0) (else (+ (count-change total (bf coinvalue) (bf coincount)) ; not use first coin type (if (> (first coincount) 0) (count-change (- total (first coinvalue)) ; use 1 coin from first type coinvalue (se (- (first coincount) 1) (bf coincount))) 0))))) ; Problem 5 (define (keep-head f) (lambda (x) (first (f x)))) ; Problem 6 (define double (lambda (x) (* x 2))) (define olympics (lambda (x) (lambda (g) (g (g x))))) ((olympics ((olympics 2) double)) double) ; Problem 7 (define (constant-fn? f s) (or (< (count s) 2) (and (equal? (f (first s)) (f (first (bf s)))) (constant-fn? f (bf s))))) ; Problem 8 (define (f x y) (/ 10 x)) (define (g) (lambda (y a) (y a))) (define (h x) (lambda () (random x))) (f 0 2) ; Applicative ; (f 0 2) ; (/ 10 0) ; error divided by 0 ; Normal ; (f 0 2) ; (/ 10 0) ; error divided by 0 (f 10 ((g) 2 2)) ; Applicative ; (f 10 ((g) 2 2)) ; (f 10 ((lambda (y a) (y a)) 2 2) ; (f 10 (2 2)) ; error 2 isn't a procedure ; Normal ; (f 10 ((g) 2 2)) ; (/ 10 10) ; 1 (h (f 0 2)) ; Applicative ; (h (f 0 2)) ; (h (/ 10 0)) ; error divided by 0 ; Normal ; (h (f 0 2)) ; (lambda () (random (f 0 2))) ; returns a procedure ((g) h 0) ; Applicative ; ((g) h 0) ; ((lambda (y a) (y a)) h 0) ; (h 0) ; (lambda () (random 0)) ; return a procedure ; Normal ; ((g) h 0) ; ((lambda (y a) (y a)) h 0) ; (h 0) ; (lambda () (random 0)) ; returns the same procedure ; Problem 9 ; (a) sentences of numbers ; (b) big >= all numbers in sent except those in s ; (c) After bring the biggest number in unsorted to sorted, it doesn't remove that number from unsorted. ; (d) 2,3 ; Problem 10 ; 1: False: Theta(n * order of growth for foo) ; 2: True: garply needs to loop at least Theta(n) times ; 3: True: foo is Thetha(n), so garply execute Theta(n), Theta(n) times ; 4: False: need to add the result from recursive call with (foo n) ; 5: True: just loop over the sentence ; 6: False: for each word, you need to loop over Theta(n) elements to check ; 7: True: take the first and loop over the rest to check