COND

36 views
Skip to first unread message

vikifor

unread,
Dec 3, 2011, 8:00:02 AM12/3/11
to Land of Lisp
Hi
I have one question.How can I put many condition in COND in some
function
Thanks

Havvy

unread,
Dec 3, 2011, 12:11:47 PM12/3/11
to Land of Lisp
(cond condition1 result1 condition2 result2 ... conditionN resultN)

conditionN just has to evaluate to a boolean. You can you boolean
operators to conjunct (compose together) multiple simple conditions.

(and (= 5 5) (!= 5 4))

[Caveat: I haven't seen Common Lisp for about half a year, so check
to see where brackets are necessary]

Gary Verhaegen

unread,
Dec 3, 2011, 12:59:56 PM12/3/11
to land-o...@googlegroups.com
It's actually (cond (condition1 result1) (condition2 result2) ...), so
for example (illustrative of cond; highly suboptimal):
(defun fibo(n)
(cond ((or (not (integerp n)) (< n 0)) (error "Arg must be a
positive integer"))
((= 0 n) 1)
((= 1 n) 1)
((= 2 n) 2)
(t (+ (fibo (1- n)) (fibo (- n 2))))))

The conditions do not really need to be boolean operations, though. As
far as I can tell, Common Lisp does not even really have the notion of
a boolean: the empty list, written as either (), '() or nil, is
considered false in a boolean context, while any other value is
considered true. There is neither a separate "False" value, nor a
separate "True" value. t is juste the object at the top of the CLOS
hierarchy, which makes it the canonical "non-nil" value.

Reply all
Reply to author
Forward
0 new messages