本文介绍了Clojure列表成员转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码:
(defn is_member [elem ilist]
(elem [])false)
(if(= elem(first(list ilist)))
(true)
(is_member elem(rest(list ilist)))
)
)
我尝试运行它:
(is_member 1' ,4))
但得到错误:
#< CompilerException java.lang.ClassCastException:java.lang.Integer不能转换为clojure.lang.IFn
有什么问题?如何解决?
谢谢。
解决方案
就像你来自一个语言比Clojure更广泛的模式匹配; ((elem [])false)
在Clojure中基本上是废话。相反,只需测试 ilist
是否为空。
还有其他一些错误,因此这里有一段代码段,尽可能接近您的预期:
(defn is_member [elem ilist]
(cond(empty?ilist)false
(= elem(first ilist))true
:else(is_member elem(rest ilist))))
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))))
这篇关于Clojure列表成员转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!