From: Ramakrishnan Muthukrishnan Date: Mon, 21 Jun 2010 12:47:13 +0000 (+0530) Subject: solution to 2.19 X-Git-Url: https://git.rkrishnan.org/pf/content/install-details.html?a=commitdiff_plain;h=d2bbe9d4146b59a97ec05adefc898e6581735d02;p=sicp.git solution to 2.19 --- diff --git a/src/sicp/ex2_19.clj b/src/sicp/ex2_19.clj new file mode 100644 index 0000000..645a4c6 --- /dev/null +++ b/src/sicp/ex2_19.clj @@ -0,0 +1,28 @@ +(ns sicp.ex2_19 + (:use [sicp utils] + [clojure test])) + +(declare no-more? first-denomination except-first-denomination) + +(defn cc [amount coin-values] + (cond (= amount 0) 1 + (or (< amount 0) (no-more? coin-values)) 0 + :else + (+ (cc amount + (except-first-denomination coin-values)) + (cc (- amount + (first-denomination coin-values)) + coin-values)))) + +(defn no-more? [lst] (empty? lst)) +(defn first-denomination [lst] (first lst)) +(defn except-first-denomination [lst] (rest lst)) + +;; tests +(def *us-coins* (list 50 25 10 5 1)) +(def *uk-coins* (list 100 50 20 10 5 2 1 0.5)) + +(deftest test-us-coins-change-for-100-cents + (are [x y] [= x y] + (cc 100 *us-coins*) 292 + (cc 100 *uk-coins*) 104561)) \ No newline at end of file