From 9387d46dc07f0da16fb263fe1b1e0768617d8b5e Mon Sep 17 00:00:00 2001 From: Denis Smet Date: Sun, 15 Oct 2023 21:37:43 +0400 Subject: [PATCH] Exercise 1.9 --- src/sicp/chapter_1/ex_1_9.clj | 20 ++++++++++++++++++++ test/sicp/chapter_1/ex_1_9_test.clj | 11 +++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/sicp/chapter_1/ex_1_9.clj create mode 100644 test/sicp/chapter_1/ex_1_9_test.clj diff --git a/src/sicp/chapter_1/ex_1_9.clj b/src/sicp/chapter_1/ex_1_9.clj new file mode 100644 index 0000000..ce055b2 --- /dev/null +++ b/src/sicp/chapter_1/ex_1_9.clj @@ -0,0 +1,20 @@ +(ns sicp.chapter-1.ex-1-9) + +; Exercise 1.9 +; Each of the following two procedures defines a method for adding two positive integers +; in terms of the procedures inc, which increments its argument by 1, and dec, which decrements its argument by 1. + +(defn plus + [a b] + (if (= a 0) + b + (inc (+ (dec a) b)))) ; linear recursive + +(defn plus-v2 + [a b] + (if (= a 0) + b + (+ (dec a) (inc b)))) ; linear iteration + +; Using the substitution model, illustrate the process generated by each procedure in evaluating (+ 4 5). +; Are these processes iterative or recursive? diff --git a/test/sicp/chapter_1/ex_1_9_test.clj b/test/sicp/chapter_1/ex_1_9_test.clj new file mode 100644 index 0000000..17ebaf2 --- /dev/null +++ b/test/sicp/chapter_1/ex_1_9_test.clj @@ -0,0 +1,11 @@ +(ns sicp.chapter-1.ex-1-9-test + (:require [clojure.test :refer :all] + [sicp.chapter-1.ex-1-9 :refer [plus plus-v2]])) + +(deftest plus-test + (is (= 3 (plus 1 2))) + ) + +(deftest plus-v2-test + (is (= 3 (plus-v2 1 2))) + )