Skip to content

Latest commit

 

History

History
42 lines (30 loc) · 719 Bytes

1.34.md

File metadata and controls

42 lines (30 loc) · 719 Bytes

1.34

Question

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.

Answer

(f f) will fail trying to apply 2 to 2:

(f f)
(f 2)
(2 2)
; The object 2 is not applicable.

The issue here is "typical": f expects g to b a procedure, not data. As MIT Scheme is dynamically typed, there is no method in the evalutor to check for parameter types: the programmer must construct their own methods, e.g.

(define (f g)
  (if (procedure? g)
    (g 2)
    (error "NOT A PROCEDURE --" g)))