SICP 1.2 exercises cont. 8

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


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s