Clojure 无法将列表传递给函数错误 PersistentList 无法转换为 clojure.lang.IFn
我有一些处理列表的函数。我有一个偶数函数,它接受列表参数并获取列表的偶数索引。 odd 函数执行相同的操作,但索引为奇数。我还有另一个函数,它合并两个排序列表,称为 merge-list,它接受两个列表作为参数。
问题出在我现在编写的函数上:合并排序。
这就是我所拥有的:
(defn merge-sort [lis]
(if (empty? (rest lis))
lis
(merge-list (merge-sort (odd(lis))) (merge-sort (even(lis))))))))
出于某种原因,我不断收到错误
java.lang.ClassCastException: clojure.lang.PercientList 无法转换为 clojure.lang.IFn
我可以传递奇怪的函数rest lis,例如这个 (odd(rest lis))
(与 Even 相同)。它运行良好,但这显然没有给我我想要的解决方案。
我对 Clojure 很陌生,所以任何建议将不胜感激。谢谢。
I have a few functions that deal with lists. I have an even function which accepts a list parameter and gets the even indexes of the list. The odd function does the same thing but with odd indexes. I also have another function that merges two sorted lists called merge-list that takes two lists as parameters.
The problem is with the function I am writing now: merge-sort.
Here is what I have:
(defn merge-sort [lis]
(if (empty? (rest lis))
lis
(merge-list (merge-sort (odd(lis))) (merge-sort (even(lis))))))))
For some reason I keep getting the error
java.lang.ClassCastException: clojure.lang.PersistentList cannot be cast to clojure.lang.IFn
I can pass the odd function rest lis like this (odd(rest lis))
(same with even). It runs fine but that obviously doesn't give me the solution I want.
I'm very new to Clojure so any tips would be appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(奇数 lis)
和(偶数 lis)
,而不是(奇数(lis))
。您希望将其作为参数传递,而不是将其作为函数调用,然后传递其结果。(odd lis)
and(even lis)
, not(odd (lis))
. You want to pass it as a parameter, not call it as a function and then pass the result of that.当 Clojure 编译器遇到列表时,它会在列表的头部查找要调用的函数或宏。
错误消息“FooClass无法转换为clojure.lang.IFn”通常意味着您在“函数位置”(列表的头部)中有一个FooClass实例,它既不是函数也不是宏。
这通常是由语法错误或有缺陷的宏引起的。如果,正如 amalloy 所建议的那样,您将列表参数括在括号中,那么这就是问题所在。
When the Clojure compiler encounters a list, it looks at the head of the list for a function or macro to invoke.
The error message "FooClass cannot be cast to clojure.lang.IFn" usually means that you have an instance of FooClass in "function position" (the head of a list) which is neither a function nor a macro.
Often this is caused by a syntax error or a buggy macro. If, as amalloy suggests, you are wrapping your list argument in parentheses, then that is the problem.