— Book notes — 2 min read
1; Implementation of SIGMA (i) from INDEX to UPPER_BOUND2(DEFINE (SUM_INT index upper_bound)3 (IF (> index upper_bound)4 0 ; Easy case5 (+6 index7 (SUM_INT (1+ index) upper_bound) ; Reduced simpler problem -- subproblem to solve for8 )9 )10)
1; Implementation of SIGMA (i²) from INDEX to UPPER_BOUND2(DEFINE (SUM_SQUARE index upper_bound)3 (IF (> index upper_bound)4 05 (+6 (SQUARE index)7 (SUM_SQUARE (1+ index) upper_bound)8 )9 )10)
1; Recursively2(DEFINE (SUMMATION term index index_stepper upper_bound)3 (IF (> index upper_bound)4 05 (+6 (term index) ; term provides term at index7 (SUMMATION8 term9 (index_stepper index)10 index_stepper11 upper_bound12 )13 )14 )15)1617; Iteratively18(DEFINE (SUMMATION term lower_bound index_stepper upper_bound)19 (DEFINE (ITER index accumulator)20 (IF (> index upper_bound)21 accumulator22 (ITER23 (index_stepper index)24 (+25 accumulator26 (term index)27 )28 )29 )30 )3132 (ITER lower_bound 0)33)
1(DEFINE (SUM_INT index upper_bound)2 (SUMMATION3 (LAMBDA (x) x)4 index5 +16 upper_bound7 )8)
1(define (sqrt x)2 (define tolerance 0.00001)34 (define (good-enough? candidate)5 (<6 (abs (- (* candidate candidate) x))7 tolerance8 )9 )1011 (define (improve candidate)12 (average13 (/ x candidate)14 candidate15 )16 )1718 (define (find-sqrt candidate)19 (if (good-enough? candidate)20 candidate21 (find-sqrt (improve candidate))22 )23 )2425 (find-sqrt 1)26)2728(define (average m n)29 (/30 (+ m n)31 232 )33)
improve(candiate) -> (candidate + (x / candidate)) / 2
improve(sqrt(x)) = sqrt(x) Example: improve(sqrt(9)) = (3 + (9 / 3)) / 2 = (3 + 3) / 2 = 3
Therefore, the fixed-point of the function improve(candidate) is what is desired.
1(define (fixed-point function start)2 3 (define tolerance 0.00001)45 ; Checks if two input are close enough as defined by the tolerance6 (define (close-enough m n)7 (<8 (abs (- m n))9 tolerance10 )11 )1213 ; Repeat until old and new are close enough14 (define (iter old new)15 (if (close-enough old new)16 new17 (iter new (function new))18 )19 )2021 (iter start (function start))22)2324(define (sqrt x)25 (fixed-point26 (lambda (b)27 (/28 (+ b (/ x b))29 230 )31 )32 133 )34)
improve(candidate) = (x / candidate)
However the example above oscilates when iterated.
when x = 2 improve(1) = 2 / 1 = 2 improve(2) = 2 / 2 = 1 improve(1) = 2 / 1 = 2
1(define (sqrt x)2 (fixed-point3 (average-damp4 (lambda (candidate)5 (/ x candidate)6 )7 )8 19 )10)1112; Higher-order procedure: Procedure that takes procedure as argument and returns a procedure as value.13(define average-damp14 (lambda (function)15 (lambda (x)16 (average (function x) x)17 )18 )19)
sqrt
function uses good-enough
function. The implementation details of good-enough
is irrelevant to sqrt
-- it could be using abs
or any other function for its implementation."I said that computer science is a lot like magic, and it's sort of good that it's like magic. There's a bad part of computer science that's a lot like religion. And in general, I think people who really believe that you design everything before you implement it basically are people who haven't designed very many things.
The real power is that you can pretend that you've made the decision and then later on figure out which one is right, which decision you ought to have made. And when you can do that, you have the best of both worlds."
~Harold Abelson, 1986