ClassCastException java.lang.Boolean不能被转换为clojure.lang.IFn [英] ClassCastException java.lang.Boolean cannot be cast to clojure.lang.IFn

查看:685
本文介绍了ClassCastException java.lang.Boolean不能被转换为clojure.lang.IFn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(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


ClassCastException java.lang.Boolean不能转换为clojure.lang.IFn user / is-member? (NO_SOURCE_FILE:28)

ClassCastException java.lang.Boolean cannot be cast to clojure.lang.IFn user/is-member? (NO_SOURCE_FILE:28)

为什么?
我理解,如果表达式被括在圆括号中,那意味着它将被作为一个函数求值。

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆