Alex Rivera | Logout

The Clojure (or Lisp) Equivalent of a Compound Boolean Test

Asked 2009-02-11T03:11:13.647
21

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.

Edit
Report

1 Answer

2

It's really cool! (and x y) is a macro -- you can check the source code at clojure.org -- that expands to (if x y false) equivalent to:

if (x) {
  if (y) {
    ...
  }
} else {
  false
}

(or x y) is similar but reversed.

answered 2010-10-29T16:20:40.327

Your Answer