;Problem Set 2 Solutions ;January 19, 2006 ;Grant Oladipo ;;;;;;;;;; ;Problem 1 ;;;;;;;;;; (display "Problem 1\n") (lambda (x y z) x) ;a. Special form type ;b. procedure ;c. # ((lambda (x y) (+ x y)) 4 (+ 3 4)) ;a. Combination (special form lambda inside evaluates to a procedure) ;b. 11 ;c. 11 ((lambda (happy tiger) (if (= tiger 4) (+ happy tiger) happy)) 4 5) ;a. Combination (special form lambda inside with special form if inside; lambda evaluates to a procedure) ;b. 4 ;c. 4 ((lambda (wow this works) (wow this works)) - 7 5) ;a. Combination (special form lambda inside evaluates to a procedure) ;b. 2 ;c. 2 ;((lambda (wow this works) (works wow this)) 7 - 5) ;a. Combination (special form lambda inside evaluates to a procedure) ;b. error ;c. procedure application: expected procedure, given: 5; arguments were: 7 # ((if (= 4 5) (lambda (x) (+ x 3)) (lambda (x) (+ x 5))) 7) ;a. Combination (special form if that returns one of two special form lambdas that evaluates to a procedures) ;b. 12 ;c. 12 (define x 2) ;a. Special form define ;b. unspecified ;c. unspecified ((lambda (x) (+ x x)) 5) ;a. Combination (special form lambda inside that evaluates to a procedure) ;b. 10 ;c. 10 ((lambda (yummy) (* yummy yummy)) 5) ;a. Combination (special form lambda inside that evaluates to a procedure) ;b. 25 ;c. 25 ;yummy ;a. name ;b. error ;c. reference to undefined identifier: yummy ;;;;;;;;;; ;Problem 2 ;;;;;;;;;; (display "\nProblem 2\n") ;a. Sign (define sign (lambda (number) (if (> number 0) 1 (if (= number 0) 0 -1)))) (sign 104) (sign 0) (sign (- 4 6)) ;b. Beautiful-Rectangle (define beautiful-rectangle (lambda (width) (* width (/ (+ (sqrt 5) 1) 2)))) (beautiful-rectangle 34.5) (beautiful-rectangle 1) ;c. Positive-Root (define positive-root (lambda (a b c) (if (> 0 (- (* b b) (* 4 a c))) "complex roots" (/ (+ (- b) (sqrt (- (* b b) (* 4 a c)))) (* 2 a))))) (positive-root 1 -2 1) (positive-root 3 1 3) ;d. Censor (define (substring? str substr) (let ((strlen (string-length str))) (if (< strlen (string-length substr)) #f (if (string=? substr (substring str 0 (string-length substr))) #t (substring? (substring str 1 strlen) substr))))) (define censor (lambda (s) (if (substring? s "scheme sucks") "BEEP" s))) (censor "I love scheme; it's so cool!") (censor "Dunno, but I think scheme sucks a lot") ;;;;;;;;;; ;Problem 3 ;;;;;;;;;; (display "\nProblem 3\n") ;a. Slow-Multiply (define slow-mul (lambda (a b) (if (= b 0) 0 (+ a (slow-mul a (- b 1)))))) (slow-mul 3 4) (slow-mul 5 9) ;b. Even (define even? (lambda (number) (if (< number 2) (= number 0) (even? (- number 2))))) (even? 24) (even? 9)