...
To see how declarations appear in the body of a function, consider the following example, which uses Newton's method to calculate square roots :2(Adapted from section 1.1.8 of Structure and Interpretation of Computer Programs.):
Code Block | ||||
---|---|---|---|---|
| ||||
sqrt = function(x) { average = function(x,y) { (x + y) / 2 }; good_enough = function(guess, x) { v = (guess * guess) - x; v < 0.01 && v > -0.01 }; improve = function(guess, x) { average(guess, (x / guess)) }; sqrt_iter = function(guess, x) { good_enough(guess, x) => guess | sqrt_iter(improve(guess,x), x) }; sqrt_iter(1.0, x) } |
...