Conrad,

The syntax of 'cond' is actually pretty straightforward. Following the symbol 
'cond' you have pairs of predicate forms and consequent expressions. The 'cond' 
form evaluates each predicate in turn until one evaluates to true and then 
returns the value of the corresponding consequent form. Many 'cond' forms have 
a default value that is returned when none of the predicates succeed. Remember 
that in Clojure only the value 'false' and the value 'nil' are considered 
false. Everything else is true. By convention we use the keyword ':else' as a 
'predicate' for the default case. Since ':else' is neither 'false' nor 'nil', 
it is considered to be true, and if the 'cond' form reaches the ':else' clause, 
then the default value will be returned.

Given these restrictions, only a) and c) are syntactically correct. And even 
they don't do what you want.

> a)
> 
> (cond
>   (= total 20) 8.75
>   (or (amount > 20) (= country "US") 9.75)
>   (else 10.0))
> 

Here we have:
predicate | consequent
(= total 20) | 8.75
(or (amount > 20) (= country "US") 9.75) |  (else 10.0)

Unfortunately, (else 10.0) winds up as the consequent of the 2nd predicate. 
Furthermore, 'else' is not a Clojure operator. Unless you've defined a function 
or macro named 'else' you will get an error.

> b)
> 
> (cond
>   (= total 20) 8.75
>   (or (amount > 20) (= country "US") 9.75)
>   :default 10.0)
> 

predicate | consequent
 (= total 20) | 8.75
 (or (amount > 20) (= country "US") 9.75) | :default
 10.0 | ???

These are not paired up properly.

> c)
> 
> (cond
>   (= total 20) 8.75
>   (or (amount > 20) (= country "US") 9.75)
>   10.0 )

predicate | consequent
 (= total 20)  | 8.75
 (or (amount > 20) (= country "US") 9.75) | 10.0

Not what you expected...

> 
> d)
> 
> (cond
>   (= total 20) 8.75
>   (or (amount > 20) (= country "US") 9.75)
>   :else 10.0 )

predicate | consequent
 (= total 20)  | 8.75
 (or (amount > 20) (= country "US") 9.75) |  :else
 10.0 | ???

Not syntactically correct.

Here's what you want to use:
(cond
  (== total 20) 8.75
  (or (> amount 20) (= country "US")) 9.75
  :else 10.0)

(Note that '==' is the proper predicate for numerical equality.)

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