On Jul 6, 2011, at 10:58 PM, Conrad Taylor wrote:

> On Jul 6, 7:33 pm, Benny Tsai <benny.t...@gmail.com> wrote:
>> Could you please post the entire form, including the code surrounding the
>> cond form (since total, amount, and country need to be defined somewhere)?
> 
> Benny, that was just sample code to zero in on the initial issue.  I'm
> working
> through the SICP with a lot of pain but here's what I have so far:
> 
> (def us-coins (list 50 25 10 5 1))
> (def uk-coins (list 100 50 20 10 5 2 1 0.5))
> 
> (defn first-denomination [ coin-values ] (first coin-values))
> 
> (defn except-first-denomination [ coin-values ] (rest coin-values))
> 
> (defn no-more? [coin-values] (nil? coin-values))
> 
> (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))))
> 

Part of the problem is that Clojure uses a slightly different syntax for 'cond' 
than Scheme (and Common Lisp) do. In particular, Common Lisp more frequently 
allows for side effects, so rather than a single consequent value Common Lisp's 
COND encloses its consequent expressions in a an additional layer of 
parentheses. Clojure discourages side effects, so it's reasonable to think in 
terms of a single expression as a consequent. The need for enclosing 
parentheses disappears.

Beware that the predicate 'nil?' tests whether an object is 'nil'. You probably 
want to use the predicate 'empty?' to test whether your coin list is empty.


Have all good days,
David Sletten




-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to