The code in my homework works without error:
(define (deriv-squared f)
(square-a-procedure (deriv f)))
But when I define it as below, it doesn't evaluate and says: "deriv as undefined" even I define it. What is the difference between two definitions?
(define deriv-squared (square-a-procedure deriv))
A variable needs to have a binding before it it is referenced (used).
If you have
(define deriv-squared (square-a-procedure deriv))
then (square-a-procedure deriv)
is evaluated and the resulting value
is given the name deriv-squared
. During the evaluation of (square-a-procedure deriv)
the value of deriv
is looked up. Therefore: the definition of deriv
must be placed before the definition of deriv-squared
.
(My guess is that you have placed the definition of deriv
further down in the source file.)
Now why did your first definition work?
(define (deriv-squared f) (square-a-procedure (deriv f)))
This is short for:
(define deriv-squared
(lambda (f)
(square-a-procedure (deriv f))))
Here the (lambda (f) (square-a-procedure (deriv f))))
evaluated to a procedure.
But deriv
is not referenced until the procedure is used. So if deriv
is defined before you use deriv-squared
everything works fine.