问题描述
(defn is-member? [a lst]
((cond
(empty? lst) false
(= a (first lst)) true
:else (is-member? a (rest lst))
)))
(is-member? :b '(:a :b :c))
当我执行上述代码时会收到错误
When I execute the above code I get error
为什么?
我理解,如果表达式被括在圆括号中,那意味着它将被作为一个函数求值。
Why?I understand that if an expression is enclosed in parentheses then that means it will be evaluated as a function..
推荐答案
你在双括号中得到cond表达式。这导致cond(true或false)的最终结果被称为函数。
You got the cond expression in double parentheses. That causes the final result of cond (true or false) to be called as a function. Fix that and it works.
=> (defn is-member?
[a lst]
(cond
(empty? lst) false
(= a (first lst)) true
:else (is-member? a (rest lst))))
#'user/is-member?
=> (is-member? :b '(:a :b :c))
true
在Clojure中最常用的方法是使用一些
。
The most idiomatic way to do this in Clojure, by the way, is using some
.
=> (some #{:b} [:a :b :c])
:b
这返回实际的第一个值,但是因为Clojure的,它可以在条件中使用的方式与true和false大致相同。
This returns the actual first value, but because of Clojure's truthiness it can be used in conditionals in much the same way as true and false.
这篇关于ClassCastException java.lang.Boolean不能被转换为clojure.lang.IFn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!