From 044a53adbba9604d5c3741999ac608f0fb9b8a7e Mon Sep 17 00:00:00 2001 From: Denis Smetannikov Date: Sun, 15 Oct 2023 20:39:56 +0300 Subject: [PATCH] Exercise 1.9 (#3) --- src/sicp/chapter_1/ex_1_9.clj | 20 ++++++++++++++++++++ test/sicp/chapter_1/ex_1_9_test.clj | 9 +++++++++ 2 files changed, 29 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..665c410 --- /dev/null +++ b/test/sicp/chapter_1/ex_1_9_test.clj @@ -0,0 +1,9 @@ +(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))))