Skip to content

Commit

Permalink
Exercise 1.20
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Smet committed Oct 15, 2023
1 parent 7d21556 commit 3135231
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/sicp/chapter_1/ex_1_20.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns sicp.chapter-1.ex-1-20)

; Exercise 1.20
; 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 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?

(defn gcd [a b]
(if (= b 0)
a
(gcd b (mod a b))))
7 changes: 7 additions & 0 deletions test/sicp/chapter_1/ex_1_20_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns sicp.chapter-1.ex-1-20-test
(:require [clojure.test :refer :all])
(:require [sicp.chapter-1.ex-1-20 :refer [gcd]]))

(deftest gcd-test
(is (= 2 (gcd 206 40))) ; 4 times
)

0 comments on commit 3135231

Please sign in to comment.