From: Ramakrishnan Muthukrishnan Date: Sun, 5 Sep 2010 17:08:04 +0000 (+0530) Subject: Fixed a bug with the rest case input of '(x + 3 * (x + y + 2)) X-Git-Url: https://git.rkrishnan.org/simplejson/...?a=commitdiff_plain;h=88e008e0e0d464ca44b87107a73f76a0a020ef27;p=sicp.git Fixed a bug with the rest case input of '(x + 3 * (x + y + 2)) --- diff --git a/src/sicp/ex2_58b.clj b/src/sicp/ex2_58b.clj index 9d2ffc9..9740003 100644 --- a/src/sicp/ex2_58b.clj +++ b/src/sicp/ex2_58b.clj @@ -101,19 +101,35 @@ (and (list? x) (= (op-expr x) '+))) (defn addend [s] - (first-expr s)) + (let [a (first-expr s)] + (if (and (list? a) + (= (count a) 1)) + (first a) + a))) (defn augend [s] - (rest-expr s)) + (let [a (rest-expr s)] + (if (and (list? a) + (= (count a) 1)) + (first a) + a))) (defn product? [x] (= (second x) '*)) (defn multiplier [p] - (first p)) + (let [m (first p)] + (if (and (list? m) + (= (count m) 1)) + (first m) + m))) (defn multiplicand [p] - (rest (rest p))) + (let [m (rest (rest p))] + (if (and (list? m) + (= (count m) 1)) + (first m) + m))) (defn make-exponentiation [b n] (cond (=number? b 1) 1