]> git.rkrishnan.org Git - sicp.git/blob - src/sicp/ex2_19.clj
Merge branch 'master' of github.com:vu3rdd/sicp
[sicp.git] / src / sicp / ex2_19.clj
1 (ns sicp.ex2_19
2   (:use [sicp utils]
3         [clojure test]))
4
5 (declare no-more? first-denomination except-first-denomination)
6
7 (defn cc [amount coin-values]
8   (cond (= amount 0) 1
9         (or (< amount 0) (no-more? coin-values)) 0
10         :else
11         (+ (cc amount
12                (except-first-denomination coin-values))
13            (cc (- amount
14                   (first-denomination coin-values))
15                coin-values))))
16
17 (defn no-more? [lst] (empty? lst))
18 (defn first-denomination [lst] (first lst))
19 (defn except-first-denomination [lst] (rest lst))
20
21 ;; tests
22 (def *us-coins* (list 50 25 10 5 1))
23 (def *uk-coins* (list 100 50 20 10 5 2 1 0.5))
24
25 (deftest test-us-coins-change-for-100-cents
26   (are [x y] [= x y]
27        (cc 100 *us-coins*) 292
28        (cc 100 *uk-coins*) 104561))