SICP-2nd-Edition-Exercise-1.34

From Boozled

Jump to: navigation, search

Contents

Exercise 1.34 From SICP

Details from SICP are here

Suppose we define the procedure

  1. (define (f g)
  2.   (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

  1. (define (f g) (g 2))
  2. (define (f2 g) (g h))
  3. (define (h g) (+ 2 6))
  4.  
  5. (define (square x) (* x x))
  6.  
  7. (display (f2 f2))
  8.  
  9. (display (f f))

Parent Course

6.001_Structure_and_Interpretation_of_Computer_Programs

Further Reading

Personal tools