From 31352310521caef8c3c0e26b3bd731b538470437 Mon Sep 17 00:00:00 2001 From: Denis Smet Date: Mon, 16 Oct 2023 00:29:37 +0400 Subject: [PATCH] Exercise 1.20 --- src/sicp/chapter_1/ex_1_20.clj | 17 +++++++++++++++++ test/sicp/chapter_1/ex_1_20_test.clj | 7 +++++++ 2 files changed, 24 insertions(+) create mode 100644 src/sicp/chapter_1/ex_1_20.clj create mode 100644 test/sicp/chapter_1/ex_1_20_test.clj diff --git a/src/sicp/chapter_1/ex_1_20.clj b/src/sicp/chapter_1/ex_1_20.clj new file mode 100644 index 0000000..ff05dfa --- /dev/null +++ b/src/sicp/chapter_1/ex_1_20.clj @@ -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)))) diff --git a/test/sicp/chapter_1/ex_1_20_test.clj b/test/sicp/chapter_1/ex_1_20_test.clj new file mode 100644 index 0000000..ffe66d9 --- /dev/null +++ b/test/sicp/chapter_1/ex_1_20_test.clj @@ -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 +)