In the following code, I am trying to understand how the variable whatami gets its value. In following the logic, I see that the procedure (lambda (y) (/ x y)) is the parameter that I am passing to the method average-damp, and is represented within that method as the variable f. It seems as though (/ x y) and (average (f whatami) whatami) need to be executed, but I can’t figure out the order of execution. Any help is appreciated.
(define (average x y)
(/ (+ x y) 2))
(define (fixed-point f start)
(define tolerance 0.00001)
(define (close-enuf? u v)
(< (abs (- u v)) tolerance))
(define (iter old new)
(if (close-enuf? old new)
new
(iter new (f new))))
(iter start (f start)))
(define average-damp
(lambda (f)
(lambda (whatami) (average (f whatami) whatami))))
; square root with average damping
(define (_sqrt x)
(fixed-point
(average-damp (lambda (y) (/ x y)))
1))
(_sqrt 4.0)
The
average-dampprocedure takes a procedure as its argument and returns a procedure as its value. When given a procedure that takes one argument,average-dampreturns another procedure that computes the average of the values before and after applying the original functionfto its argument. It’s inside thefixed-pointprocedure where that returned function is applied (iteratively).So the
average-dampprocedure doesn’t execute either(/ x y)or(average(f whatami) whatami)at all, it just uses the function passed to it to create a new function that it returns.