Skip to content

Latest commit

 

History

History
124 lines (95 loc) · 4.38 KB

1.20.md

File metadata and controls

124 lines (95 loc) · 4.38 KB

1.20

Question

The process that a procedure generates is of course dependent on the rules used by the interpreter. As an example, consider the iterative gcd procedure given above. Suppose we were to interpret this procedure using normal-order evaluation, as discussed in section 1.1.5. (The normal-order-evaluation rule for if is described in exercise 1.5.) Using the substitution method (for normal order), illustrate the process generated in evaluating (gcd 206 40) and indicate the remainder operations that are actually performed. How many remainder operations are actually performed in the normal-order evaluation of (gcd 206 40)? In the applicative-order evaluation?

Answer

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

Normal-order

Because it passes expressions instead of values, normal-order evaluation applies remainder 18 times.

(gcd 206 40)
;; (define (gcd 206 40)
;;   (if (= 40 0)
;;       206
;;       (gcd 40 (remainder 206 40)))) ; -->
;; Running `remainder` count: 0

(gcd 40 (remainder 206 40))
;; (define (gcd 40 (remainder 206 40))
;;   (if (= (remainder 206 40) 0) ; 1
;;       40
;;       (gcd (remainder 206 40) (remainder 40 (remainder 206 40))))) ; -->
;; Running `remainder` count: 1

(gcd (remainder 206 40) (remainder 40 (remainder 206 40)))
;; (define (gcd (remainder 206 40) (remainder 40 (remainder 206 40)))
;;   (if (= (remainder 40 (remainder 206 40)) 0) ; 2
;;       (remainder 206 40)
;;       (gcd (remainder 40 (remainder 206 40)) (remainder (remainder 206 40) (remainder 40 (remainder 206 40)))))) ; -->
;; Running `remainder` count: 3

(gcd (remainder 40 (remainder 206 40)) (remainder (remainder 206 40) (remainder 40 (remainder 206 40))))
;; (define (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) ; 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))))))) ; -->
;; Running `remainder` count: 7

(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)))))
;; (define (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) ; 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))))))))
;; Running `remainder` count: 14

(remainder (remainder 206 40) (remainder 40 (remainder 206 40)))
;; Running `remainder` count: 18

2
;; Running `remainder` count: 18

Applicative-order

Applicative-order saves steps by passing values, only applying remainder 4 times.

(gcd 206 40)
;; (define (gcd 206 40)
;;   (if (= 40 0)
;;       206
;;       (gcd 40 (remainder 206 40)))) ; -->
;; Running `remainder` count: 0

(gcd 40 (remainder 206 40))
;; Running `remainder` count: 0

(gcd 40 6) ; 1
;; Running `remainder` count: 1

;; (define (gcd 40 6)
;; (if (= 6 0)
;;   40
;;   (gcd 6 (remainder 40 6))) ; -->
;; Running `remainder` count: 1

(gcd 6 (remainder 40 6))
;; Running `remainder` count: 1

(gcd 6 4) ; 1
;; Running `remainder` count: 2

;; (define (gcd 6 4)
;; (if (= 4 0)
;;   6
;;   (gcd 4 (remainder 6 4))) ; -->
;; Running `remainder` count: 2

(gcd 4 (remainder 6 4))
;; Running `remainder` count: 3

(gcd 4 2) ; 1
;; Running `remainder` count: 3

;; (define (gcd 4 2)
;; (if (= 2 0)
;;   4
;;   (gcd 2 (remainder 4 2))) ; -->
;; Running `remainder` count: 3

(gcd 2 (remainder 4 2))
;; Running `remainder` count: 3

(gcd 2 0) ; 1
;; Running `remainder` count: 4

;; (define (gcd 2 0)
;; (if (= 0 0)
;;   2 ; -->
;;   (gcd 0 (remainder 2 0)))
;; Running `remainder` count: 4

2
;; Running `remainder` count: 4