# SICP 1.2 exercises cont. 8

**Posted:**January 14, 2013

**Filed under:**Improving, SICP Leave a comment

**1.20**

(define (gcd a b) (if (= b 0) a (gcd b (remainder a b)))) (gcd 206 40) (if (= 40 0) 40 (gcd 40 (remainder 206 40))) (gcd 40 (remainder 206 40)) (if (= (remainder 206 40) 0) 40 (gcd (remainder 206 40) (remainder 40 (remainder 206 40)))) (if (= 6 0) ; +1 40 (gcd (remainder 206 40) (remainder 40 (remainder 206 40)))) (gcd (remainder 206 40) (remainder 40 (remainder 206 40))) (if (= (remainder 40 (remainder 206 40)) 0) (remainder 206 40) (gcd (remainder 206 40) (remainder (remainder 206 40) (remainder 40 (remainder 206 40))))) (if (= 4 0) ; +2 (remainder 206 40) (gcd (remainder 206 40) (remainder (remainder 206 40) (remainder 40 (remainder 206 40))))) (gcd (remainder 40 (remainder 206 40)) (remainder (remainder 206 40) (remainder 40 (remainder 206 40))) (if (= (remainder (remainder 206 40) (remainder 40 (remainder 206 40))) 0) (remainder 40 (remainder 206 40)) (gcd (remainder (remainder 206 40) (remainder 40 (remainder 206 40))) (remainder (remainder 40 (remainder 206 40)) (remainder (remainder 206 40) (remainder 40 (remainder 206 40))) (if (= 2 0) ; +4 (remainder 40 (remainder 206 40)) (gcd (remainder (remainder 206 40) (remainder 40 (remainder 206 40))) (remainder (remainder 40 (remainder 206 40)) (remainder (remainder 206 40) (remainder 40 (remainder 206 40))) (gcd (remainder (remainder 206 40) (remainder 40 (remainder 206 40))) (remainder (remainder 40 (remainder 206 40)) (remainder (remainder 206 40) (remainder 40 (remainder 206 40))))) (if (= (remainder (remainder 40 (remainder 206 40)) (remainder (remainder 206 40) (remainder 40 (remainder 206 40)))) 0) (remainder (remainder 206 40) (remainder 40 (remainder 206 40))) (gcd (remainder (remainder 40 (remainder 206 40)) (remainder (remainder 206 40) (remainder 40 (remainder 206 40)))) (remainder (remainder (remainder 206 40) (remainder 40 (remainder 206 40))) (remainder (remainder 40 (remainder 206 40)) (remainder (remainder 206 40) (remainder 40 (remainder 206 40))))))) (if (= 0 0) ; +7 (remainder (remainder 206 40) (remainder 40 (remainder 206 40))) (gcd (remainder (remainder 40 (remainder 206 40)) (remainder (remainder 206 40) (remainder 40 (remainder 206 40)))) (remainder (remainder (remainder 206 40) (remainder 40 (remainder 206 40))) (remainder (remainder 40 (remainder 206 40)) (remainder (remainder 206 40) (remainder 40 (remainder 206 40))))))) (remainder (remainder 206 40) (remainder 40 (remainder 206 40))) 2 ; +4

Total 18 calls to remainder in normal-order evaluation.

(gcd 206 40) (if (= 40 0) 40 (gcd 40 (remainder 206 40))) (gcd 40 (remainder 206 40)) (gcd 40 6) ; +1 (if (= 6 0) 40 (gcd 6 (remainder 40 6))) (gcd 6 (remainder 40 6)) (gcd 6 4) ;+1 (if (= 4 0) 6 (gcd 4 (remainder 6 4))) (gcd 4 2) ; +1 (if (= 2 0) 4 (gcd 2 (remainder 4 2))) (gcd 2 (remainder 4 2)) (gcd 2 0); +1 (if (= 0 0) 2 (gcd 0 (remainder 2 0))) 2

Total 4 calls to remainder in applicative-order evaluation.

**Comments:** Evaluating in normal order was pretty tedious. Having a nice editor like emacs really helped me here.

Advertisements