I'm going through the exercises in [SICP][1] and am wondering if someone can explain the difference between these two seemingly equivalent functions that are giving different results! Is this because of rounding?? I'm thinking the order of functions shouldn't matter here but somehow it does? Can someone explain what's going on here and why it's different?
Details:
Exercise 1.45: ..saw that finding a fixed point of
y => x/y
does not converge, and that this can be fixed by average damping. The same method works for finding cube roots as fixed points of the average-dampedy => x/y^2
. Unfortunately, the process does not work for fourth roots—a single average damp is not enough to make a fixed-point search fory => x/y^3
converge.On the other hand, if we average damp twice (i.e., use the average damp of the average damp of
y => x/y^3
) the fixed-point search does converge. Do some experiments to determine how many average damps are required to compute nth roots as a fixed-point search based upon repeated average damping ofy => x/y^(n-1)
.Use this to implement a simple procedure for computing the roots using
fixed-point
,average-damp
, and therepeated
procedure of Exercise 1.43. Assume that any arithmetic operations you need are available as primitives.
My answer (note order of repeat
and average-damping
):
(define (nth-root-me x n num-repetitions) (fixed-point (repeat (average-damping (lambda (y) (/ x (expt y (- n 1))))) num-repetitions) 1.0))
I see an alternate web solution where repeat
is called directly on average damp
and then that function is called with the argument
(define (nth-root-web-solution x n num-repetitions) (fixed-point ((repeat average-damping num-repetition) (lambda (y) (/ x (expt y (- n 1))))) 1.0))
Now calling both of these, there seems to be a difference in the answers and I can't understand why! My understanding is the order of the functions shouldn't affect the output (they're associative right?), but clearly it is!
> (nth-root-me 10000 4 2)
>
> 10.050110705350287
>
> (nth-root-web-solution 10000 4 2)
>
> 10.0
I did more tests and it's always like this, my answer is close, but the other answer is almost always closer! Can someone explain what's going on? Why aren't these equivalent? My guess is the order of calling these functions is messing with it but they seem associative to me.
For example:
(repeat (average-damping (lambda (y) (/ x (expt y (- n 1)))))
num-repetitions)
vs
((repeat average-damping num-repetition)
(lambda (y) (/ x (expt y (- n 1)))))
Other Helper functions:
(define (fixed-point f first-guess)
(define (close-enough? v1 v2)
(< (abs (- v1 v2))
tolerance))
(let ((next-guess (f first-guess)))
(if (close-enough? next-guess first-guess)
next-guess
(fixed-point f next-guess))))
(define (average-damping f)
(lambda (x) (average x (f x))))
(define (repeat f k)
(define (repeat-helper f k acc)
(if (<= k 1)
acc
;; compose the original function with the modified one
(repeat-helper f (- k 1) (compose f acc))))
(repeat-helper f k f))
(define (compose f g)
(lambda (x)
(f (g x))))