Skip to content

Latest commit

 

History

History
80 lines (63 loc) · 1.72 KB

1.20.md

File metadata and controls

80 lines (63 loc) · 1.72 KB

問題 1.20 解答

(define (gcd a b)
  (if (= b 0)
    a
    (gcd b (remainder a b))))

作用的順序の評価

(gcd 206 40)
(gcd 40 (remainder 206 40))
(gcd 40 6)
(gcd 6 (remainder 40 6))
(gcd 6 4)
(gcd 4 (remainder 6 4))
(gcd 4 2)
(gcd 2 (remainder 4 2))
(gcd 2 0)
2

remainderは4回実行される。

正規順序の評価

(gcd 206 40)

(if (= 40 0) ...)

(gcd 40
     (remainder 206 40))

(if (= (remainder 206 40) 0) ...)
(if (= 6 0) ...)

(gcd (remainder 206 40)
     (remainder 40
                (remainder 206 40)))

(if (= (remainder 40 (remainder 206 40)) 0) ...)
(if (= 4 0) ...)

(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) ...)
(if (= 2 0) ...)

(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) ...)
(if (= 0 0) ...)

(remainder (remainder 206 40)
           (remainder 40
                      (remainder 206 40)))
2

ifの述語評価で14回、最後にaを返すときに4回、remainderは合計18回実行される。