SICP-2nd-Edition-Exercise-1.34
From Boozled
Contents |
Exercise 1.34 From SICP
Suppose we define the procedure
(define (f g)
(g 2))
Then we have
(f square) 4
(f (lambda (z) (* z (+ z 1)))) 6
What happens if we (perversely) ask the interpreter to evaluate the combination (f f)? Explain.
Attempt
The function f is expecting a function as an argument. So as the function f is evaluated and (f 2) gets called it fails because 2 is not a function. Note how I have got around this with f2. If I give it a function as expected it behaves normally.
Scheme
(define (f g) (g 2))
(define (f2 g) (g h))
(define (h g) (+ 2 6))
(define (square x) (* x x))
(display (f2 f2))
(display (f f))
Parent Course
6.001_Structure_and_Interpretation_of_Computer_Programs

