Skip to content

Commit

Permalink
Exercise 1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Smet committed Oct 15, 2023
1 parent c64addb commit 9387d46
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/sicp/chapter_1/ex_1_9.clj
Original file line number Diff line number Diff line change
@@ -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?
11 changes: 11 additions & 0 deletions test/sicp/chapter_1/ex_1_9_test.clj
Original file line number Diff line number Diff line change
@@ -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)))
)

0 comments on commit 9387d46

Please sign in to comment.