I was struggling with flattening a tree structure. I was doing it recursively by comparing each atomic symbol to the rest in the tree but, a friend of mine suggested the followi
((atom tree)(list tree))
is a piece of syntax which cannot be understood on its own. It belongs to the cond
syntax: it is known in Lisp culture as a cond pair. The cond pair says that if the (atom tree)
expression is true, then (list tree)
is evaluated. cond
then terminates and returns that value.
The cond pair terminology is imprecise because in Common Lisp, the cond clauses are n-ary. They do not have to be pairs. The additional forms can be omitted entirely, so that the clauses are singleton items, allowing cond
to be used like or
:
(cond (a) (b) (c) ...) <==> (or a b c)
Here, a
is evaluated. If it yields true (a value other than nil), then cond
stops and returns the value of a
. Otherwise it tests b
and so on. That's exactly what the or
does.
There can be two or more forms:
(cond (a p r s) (b t u) (c x y) ...)
If a
yields true, then p
r
and s
are evaluated, and the value of s
is returned out of cond
. If a
yields false, then b
is tried and so on.
cond
is basically a generalized or
, for which you pay with extra parentheses: try this case, or else try that case, etc.