]> git.rkrishnan.org Git - sicp.git/blob - src/sicp/ex1_9.clj
Solutions to 4.27, 4.28 and 4.29.
[sicp.git] / src / sicp / ex1_9.clj
1 (ns sicp.ex1_9
2   (:use [sicp utils]
3         [clojure.contrib trace test-is]))
4
5 ;; exercise 1.9
6 (defn ++ [a b]
7   (if (= a 0)
8     b
9     (inc (++ (dec a) b))))
10
11 ;; (comment
12 ;;   This version is a recursive process, where the previous call increments
13 ;;   the sum by 1 and each call decrement the first operand by 1.
14   
15 ;; user> (dotrace [++] (++ 4 5))
16 ;; TRACE t3745: (++ 4 5)
17 ;; TRACE t3746: |    (++ 3 5)
18 ;; TRACE t3747: |    |    (++ 2 5)
19 ;; TRACE t3748: |    |    |    (++ 1 5)
20 ;; TRACE t3749: |    |    |    |    (++ 0 5)
21 ;; TRACE t3749: |    |    |    |    => 5
22 ;; TRACE t3748: |    |    |    => 6
23 ;; TRACE t3747: |    |    => 7
24 ;; TRACE t3746: |    => 8
25 ;; TRACE t3745: => 9
26 ;; 9
27 ;; )
28
29 (defn ++ [a b]
30   (if (= a 0)
31     b
32     (++ (dec a) (inc b))))
33
34 ;; (comment
35   
36 ;; user> (dotrace [++] (++ 4 5))
37 ;; TRACE t3766: (++ 4 5)
38 ;; TRACE t3767: |    (++ 3 6)
39 ;; TRACE t3768: |    |    (++ 2 7)
40 ;; TRACE t3769: |    |    |    (++ 1 8)
41 ;; TRACE t3770: |    |    |    |    (++ 0 9)
42 ;; TRACE t3770: |    |    |    |    => 9
43 ;; TRACE t3769: |    |    |    => 9
44 ;; TRACE t3768: |    |    => 9
45 ;; TRACE t3767: |    => 9
46 ;; TRACE t3766: => 9
47 ;; 9
48 ;; )