-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |