What, exactly, is a "clause" in clojure? From seeing the term used in
context, I inferred that it meant something along the lines of "a
keyword which can optionally be used to change the semantics of a
form". Apparently, it means more than that.
This is valid clojure:
(defn sign [x] (cond (> x 0) "Positive" (< x 0) "Negative" :else
"Zero"))
I had no idea that I could use :else for the fallthrough case of a
"cond", so I looked it up in the docs:
user> (doc cond)
-------------------------
clojure.core/cond
([& clauses])
Macro
Takes a set of test/expr pairs. It evaluates each test one at a
time. If a test returns logical true, cond evaluates and returns
the value of the corresponding expr and doesn't evaluate any of the
other tests or exprs. (cond) returns nil.
Apparently, each "test/expr pair" is a clause, or perhaps each test
and each expression is a clause. No mention of supporting an :else
"clause" though. Let's check out the source code:
(defmacro cond
"Takes a set of test/expr pairs. It evaluates each test one at a
time. If a test returns logical true, cond evaluates and returns
the value of the corresponding expr and doesn't evaluate any of the
other tests or exprs. (cond) returns nil."
[& clauses]
(when clauses
(list 'if (first clauses)
(if (next clauses)
(second clauses)
(throw (IllegalArgumentException.
"cond requires an even number of forms")))
(cons 'clojure.core/cond (next (next clauses))))))
I don't know much about macros, but it is clear that "cond" builds on
"if". However, the documentation for "if" mentions nothing
about :else, and the source for "if" is not available, presumably
because it's implemented in java.
Is there a way to have discovered the existence of :else from the
documentation alone? Is this just an oversight in the docs, or is
there a more general point concerning clauses that I'm missing?
Mike
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en