Skip to content

Commit

Permalink
Exercise 1.9 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis authored Oct 15, 2023
1 parent c64addb commit 044a53a
Show file tree
Hide file tree
Showing 2 changed files with 29 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?
9 changes: 9 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,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))))

0 comments on commit 044a53a

Please sign in to comment.