Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException java.lang.Boolean cannot be cast to clojure.lang.IFn

Tags:

clojure

(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 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..

like image 848
basav Avatar asked Jan 28 '13 09:01

basav


1 Answers

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

The most idiomatic way to do this in Clojure, by the way, is using some.

=> (some #{:b} [:a :b :c])
:b

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.

like image 59
NielsK Avatar answered Nov 07 '22 21:11

NielsK