In Common Lisp and Scheme
(and (= a something) (= b another) (foo))
http://stackoverflow.com/questions/535277/the-clojure-or-lisp-equivalent-of-a-compound-boolean-test
http://stackoverflow.com/questions/5531964/non-macro-versions-of-clojure-and-and-or
In C++ I'd write something like this:
if (a == something && b == anotherthing)
{
foo();
}
Am I correct in thinking the Clojure equivalent is something like this:
(if (= a something)
(if (= b anotherthing)
(foo)))
Or is there another way to perform a logical "and" that I've missed? As I said the latter form seems to work correctly--I was just wondering if there's some simpler way to perform the logical and. And searching for "boolean" "logical" and "and" on the Clojure Google Group turned up too many results to be much use.
In Common Lisp and Scheme
(and (= a something) (= b another) (foo))
or
The function some
"Returns the first logical true value of (pred x) for any x in coll, else nil."
So you could use (some identity coll)
for or
. Note that its behaviour will differ from or
when the last value is false
: it will return nil
where or
would return false
.
and
If you don't need to know the value of the last form in the coll
vector, you can use (every? identity coll)
for and
. This will differ from the behaviour of the and
macro in that it returns true
if all of its arguments are truthy. See larsmans' answer if you need the result of the last form.