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.
(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)))