Clojure list member cast error
I try to write function thats return true if element exist in list and false if not.
My code:
(defn is_member [elem ilist]
((elem []) false)
(if (= elem (first (list ilist)))
(true)
(is_member elem (rest (list ilist)))
开发者_开发问答)
)
I try to run it:
(is_member 1 '(1,2,3,4))
But get error:
#<CompilerException java.lang.ClassCastException: java.lang.Integer cannot be cast to clojure.lang.IFn
What's wrong? How can i fix it?
Thank you.
Looks like you're coming from a language with more extensive pattern-matching than Clojure has; ((elem []) false)
is basically nonsense in Clojure. Instead, just test whether ilist
is empty.
There are a number of other errors, so here's a snippet that actually works while being as close to what you intended as possible:
(defn is_member [elem ilist]
(cond (empty? ilist) false
(= elem (first ilist)) true
:else (is_member elem (rest ilist))))
精彩评论