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

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.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值