Rationale behind case / cond difference?

815 views
Skip to first unread message

Sean Corfield

unread,
Mar 21, 2011, 1:42:55 PM3/21/11
to cloju...@googlegroups.com
This came up on Twitter and it made me curious...

cond requires an even number of clauses: (cond test1 value1 test2
value2 :else default-result) with the convention being to use :else as
the last testN expression to provide a 'placeholder' for the default /
else return value.

case allows an odd number of clauses: (case expr val1 result1 val2
result2 default-result) and the case macro pulls out (last clauses) if
(odd? (count clauses)) so it's a very explicit choice.

I can see why case has to do that - no unique value could be used as a
'default' placeholder without potentially removing a valid value from
the domain of test values - but I can't see any immediate reason why
cond couldn't treat an odd number of clauses as being the default
result: (cond test1 value1 test2 value2 default-result)

Could someone explain the rationale behind this difference?

Thanx!
--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/
Railo Technologies, Inc. -- http://www.getrailo.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

ataggart

unread,
Mar 23, 2011, 12:44:49 AM3/23/11
to Clojure Dev
I've always considered cond to be a multiplicity of if (and how one
would perform else-ifs), thus returning nil when none of the
conditions matches. Contrast that with case and condp where the test
constants/expressions form a set within which the tested expression
must fit, thus throwing an exception when that constraint is violated
is the safe default.

But I'm probably wrong.



On Mar 21, 10:42 am, Sean Corfield <seancorfi...@gmail.com> wrote:
> This came up on Twitter and it made me curious...
>
> cond requires an even number of clauses: (cond test1 value1 test2
> value2 :else default-result) with the convention being to use :else as
> the last testN expression to provide a 'placeholder' for the default /
> else return value.
>
> case allows an odd number of clauses: (case expr val1 result1 val2
> result2 default-result) and the case macro pulls out (last clauses) if
> (odd? (count clauses)) so it's a very explicit choice.
>
> I can see why case has to do that - no unique value could be used as a
> 'default' placeholder without potentially removing a valid value from
> the domain of test values - but I can't see any immediate reason why
> cond couldn't treat an odd number of clauses as being the default
> result: (cond test1 value1 test2 value2 default-result)
>
> Could someone explain the rationale behind this difference?
>
> Thanx!
> --
> Sean A Corfield -- (904) 302-SEAN
> An Architect's View --http://corfield.org/
> World Singles, LLC. --http://worldsingles.com/
> Railo Technologies, Inc. --http://www.getrailo.com/
Reply all
Reply to author
Forward
0 new messages