;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; U. C. Berkeley ;; ;; EECS Computer Science Division ;; ;; CS3 Lecture 09 (Lambdas) Answers ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;; ;; Add-letters ;;;;;;;;;;;;;; (add-letters 'phd '(garcia yelick cosby)) ;; ==> (garcia-phd yelick-phd cosby-phd) ;; How do we write add-letters? What pattern is it? (define (add-letters letters names) ;;;;;;;;; ;; Square ;;;;;;;;; (define (square x) (* x x)) ;; ==> square ;; Now let's redefine square using lambdas... (define square (lambda (x) (* x x))) ;; ==> square ( square 3 ) ;; ==> 9 ( (lambda (x) (* x x)) 3 ) ;; ==> 9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Lambda with multiple or no args ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (lambda () 'cal) ;; ==> #[procedure #x18F0794] ((lambda () 'cal)) ;; ==> cal ((lambda (a b) (+ a b)) 4 5) ;; ==> 9 ;;;;;;;;;;;;;;;;;;;;;; ;; The truth about let ;;;;;;;;;;;;;;;;;;;;;; (let ((a 4) (b 5)) (+ a b)) ;; ==> 9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Returning a procedure -- make-bookender ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (make-bookender bookend) (lambda (w) (word bookend w bookend))) ((make-bookender 'o) 'hi) ;; ==> ohio ((make-bookender 'to) 'n) ;; ==> tonto (define to_to (make-bookender 'to)) ;; ==> to_to (to_to 'ron) ;; ==> toronto ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Returning a procedure -- compose ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (compose f g) (lambda (arg) (f (g arg)))) ;; ==> compose (define second (compose first bf)) ;; ==> second (second 'abc) ;; ==> b ;;;;;;;;;;;;;;;;;; ;; HOFs - squaring ;;;;;;;;;;;;;;;;;; (every square '(1 2 3 4)) ;; ==> (1 4 9 16) (every (lambda (x) (* x x)) '(1 2 3 4)) ;; ==> (1 4 9 16) ;;;;;;;;;;;;;;;; ;; add-1-to-sent ;;;;;;;;;;;;;;;; ;; The long way (define (add-1-to-sent S) (every add-1 S)) (define (add-1 num) (+ num 1)) (add-1-to-sent '(1 2 3)) ;; ==> (2 3 4) ;; The short way (define (add-1-to-sent S) (every (lambda (num) (+ num 1)) S)) (add-1-to-sent '(1 2 3)) ;; ==> (2 3 4) ;;;;;;;;;;;;;;;; ;; add-n-to-sent ;;;;;;;;;;;;;;;; ;; add-n-to-sent ;; ;; Example: (add-n-to-sent 5 '(1 2 3)) ==> (6 7 8) (define (add-n-to-sent n S) (every add-n S)) ;; First dead-end attempt (define (add-n num) (+ num n)) (add-n-to-sent 5 '(1 2 3)) ;; ==> *** ERROR IN add-n -- Unbound variable: n ;; Why is this wrong? ;; ==> n is not known within add-n, ;; ==> only within add-n-to-sent ;; Second dead-end attempt (define (add-n num n) (+ num n)) (add-n-to-sent 5 '(1 2 3)) ;; ==> *** ERROR -- Wrong number of arguments passed to procedure ;; Why is this wrong? ;; ==> add-n can only have one argument! ;; Third (and final) attempt (define (add-n-to-sent n S) (every (lambda (num) (+ num n)) S)) (add-n-to-sent 5 '(1 2 3)) ;; ==> (6 7 8) ;; Is lambda just convenient or is it necessary? ;; ==> necessary, as far as you know ;;;;;;;;;;;;;;;;;;;;;;;; ;; Add-letters revisited ;;;;;;;;;;;;;;;;;;;;;;;; (add-letters 'phd '(garcia yelick cosby)) ;; ==> (Garcia-phd yelick-phd cosby-phd) ;; How do we write add-letters? What pattern is it? (define (add-letters letters names) (every (lambda (name) (word name '- letters)) names)) ;;;;;;;;;;;;;;;; ;; add-1-to-odds ;;;;;;;;;;;;;;;; ;; Example: ;; ;; (add-1-to-odds '(1 out of 3 likes u 2 and u b 40)) ;; ==> (2 out of 4 likes u 2 and u b 40) (define (add-1-to-odds S) (every (lambda (w) (if (and (number? w) (odd? w)) (+ 1 w) w)) S)) (add-1-to-odds '(1 out of 3 likes u 2 and u b 40)) ;; ==> (2 out of 4 likes u 2 and u b 40) ;; Is lambda just convenient or is it necessary? ;; ==> just convenient ;;;;;;;;;;;; ;; all-letter-twister? ;;;;;;;;;;;; ;; Example: ;; ;; (all-letter-twister? 'r '(rubber bumper baby buggy)) ==> #f ;; (all-letter-twister? 's '(six sheiks sixth sheeps sick)) ==> #t (define (all-letter-twister? letter S) (all? (lambda (word) (equal? (first word) letter)) S)) (all-letter-twister? 'r '(rubber bumper baby buggy)) ;; ==> #f (all-letter-twister? 's '(six sheiks sixth sheeps sick)) ;; ==> #t ;; Is lambda just convenient or is it necessary? ;; ==> necessary, as far as you know