如何抽象列表以在Clojure中的其他功能中对其进行操作?
我是功能语言的新手,我正在尝试在Clojure中实现一个简单的附加功能。我的代码:
(defn append
[lizt1 lizt2]
(cond
(empty? lizt1) lizt2
:else (def sq (cons (first sq lizt1) (append (rest sq lizt1) lizt2)))))
(let [x (quote (1 2 3))
y (quote (4 5 6))]
(append (x y))) )
我正在收到一个
clojure.lang.PersistentList cannot be cast to clojure.lang.IFn
异常,因此我假设我试图传递该功能的列表不是兼容类型(例如,如果我要执行列表功能,并尝试通过该函数)。我认为引用列表可以通过接口函数访问它。有人可以帮助我概念化如何抽象列表以在单独的函数中操纵其数据吗?
I'm very new to functional languages and I'm attempting to implement a simple append function in Clojure. My code:
(defn append
[lizt1 lizt2]
(cond
(empty? lizt1) lizt2
:else (def sq (cons (first sq lizt1) (append (rest sq lizt1) lizt2)))))
(let [x (quote (1 2 3))
y (quote (4 5 6))]
(append (x y))) )
I'm receiving an
clojure.lang.PersistentList cannot be cast to clojure.lang.IFn
exception so I'm assuming that the list that I'm attempting to pass to the function is not of compatible type(like if I were to execute a list function, and try to pass that). I thought that quoting the list would make it accessible by interface functions. Can someone help me conceptualize how to abstract a list in order to manipulate its data in a separate function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误来源是此调用:
此表达式不调用
带有参数的附加
x
和y
,但调用x 使用参数
y
,此调用的结果用作附录
的参数。但是
x
是列表,列表不能用作函数 - 这是clojure.lang.persistentlist的含义,无法将其施加到clojure.lang.ifn
。在clojure中,括号的位置非常重要且正确的语法,“ call
用
和x
xy
” 是:但是还有更多修复:
QUOTE> QUOTE
通常会缩短为':
但是我建议您使用矢量,这样:
让
s,一个让
可以具有许多绑定:cond
只有两个分支是如果
。
def 内部
defn
-def
def 创建一个全局变量。当您确实需要在功能中创建一些变量时,请使用让
。解决方案您正在寻找这样的外观:(
seq
,但是
也有效。)empty?
注意 。有一个函数
concat
concat 做同样的事情。The source of error is this call:
This expression doesn't call
append
with argumentsx
andy
, but callsx
with argumenty
and the result of this call is used as an argument forappend
.But
x
is a list and list can't be used as a function- that's the meaning ofclojure.lang.PersistentList cannot be cast to clojure.lang.IFn
.In Clojure, position of parentheses is very important and correct syntax for "call
append
withx
andy
" would be:But there is much more to fix:
quote
is usually shortened as'
:But I suggest you to use vectors, like this:
let
s here, onelet
can have many bindings:Cond
with only two branches isif
.def
insidedefn
-def
creates a global variable. When you really need to create some variables inside function, uselet
.Solution you're looking for looks like this:
(There's an even more idiomatic version with
seq
, butempty?
also works.)Note that there's a function
concat
which does exactly the same thing.