#lang planet neil/sicp (define (square x) (* x x)) (define (fast-expt b n) (define (iter a b n) (cond ((= n 0) a) ((even? n) (iter a (square b) (/ n 2))) (else (iter (* a b) b (- n 1))))) (iter 1 b n))
(define (multi a b) (define (fast-multi-iter x y n) (cond ((= n 0) x) ((even? n) (fast-multi-iter x (double y) (/ n 2))) (else (fast-multi-iter (+ x y) y (- n 1))))) (fast-multi-iter 0 a b))
Applicative order:
gcd(206, 40) gcd(40, remainder(206, 40)) ;1 gcd(6, remainder(40, 6)) ;2 gcd(4, remainder(6, 4)) ;3 gcd(2, remainder(4, 2)) ;4
For normal order, the remainder function is not calculated instantly. It is calculated when it is necessary. It is passed by as a delay item.
remainder
delay
(gcd 206 40) (gcd 40 (remainder 206 40)) -> (if (= (remainder 206 40) 0) 40 (gcd (remainder 206 40) (remainder 40 (remainder 206 40)))) -> (if (= (remainder 40 (remainder 206 40)) 0) (remainder 206 40) (gcd (remainder 40 (remainder 206 40)) (remainder (remainder 206 40) (remainder 40 (remainder 206 40))))) ...