如何抽象列表以在Clojure中的其他功能中对其进行操作?

发布于 2025-01-22 00:32:38 字数 496 浏览 2 评论 0原文

我是功能语言的新手,我正在尝试在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的十字路口 2025-01-29 00:32:38

错误来源是此调用:

(append(x y))

此表达式不调用带有参数的附加 xy,但调用x 使用参数y,此调用的结果用作附录的参数。

但是x是列表,列表不能用作函数 - 这是clojure.lang.persistentlist的含义,无法将其施加到clojure.lang.ifn

在clojure中,括号的位置非常重要且正确的语法,“ call x xy” 是:

(append x y)

但是还有更多修复:

'(1 2 3)
=> (1 2 3)

但是我建议您使用矢量,这样:

[1 2 3]
=> [1 2 3]
  • 您不必在此处使用两个嵌套 s,一个 可以具有许多绑定:
(let [x [1 2 3]
      y [4 5 6]]
  ...)
  • cond 只有两个分支是 如果
  • 不要使用 def 内部 defn - def def 创建一个全局变量。当您确实需要在功能中创建一些变量时,请使用

解决方案您正在寻找这样的外观:(

(defn append [col1 col2]
  (if (empty? col1) 
    col2
    (cons (first col1)
          (append (rest col1) col2))))
                    
(let [x [1 2 3]
      y [4 5 6]]
  (append x y))
=> (1 2 3 4 5 6)

seq,但是 empty? 也有效。)

注意 。有一个函数 concat concat 做同样的事情。

The source of error is this call:

(append(x y))

This expression doesn't call append with arguments x and y, but calls x with argument y and the result of this call is used as an argument for append.

But x is a list and list can't be used as a function- that's the meaning of clojure.lang.PersistentList cannot be cast to clojure.lang.IFn.

In Clojure, position of parentheses is very important and correct syntax for "call append with x and y" would be:

(append x y)

But there is much more to fix:

  • quote is usually shortened as ':
'(1 2 3)
=> (1 2 3)

But I suggest you to use vectors, like this:

[1 2 3]
=> [1 2 3]
  • You don't have to use two nested lets here, one let can have many bindings:
(let [x [1 2 3]
      y [4 5 6]]
  ...)
  • Cond with only two branches is if.
  • Don't use def inside defn- def creates a global variable. When you really need to create some variables inside function, use let.

Solution you're looking for looks like this:

(defn append [col1 col2]
  (if (empty? col1) 
    col2
    (cons (first col1)
          (append (rest col1) col2))))
                    
(let [x [1 2 3]
      y [4 5 6]]
  (append x y))
=> (1 2 3 4 5 6)

(There's an even more idiomatic version with seq, but empty? also works.)

Note that there's a function concat which does exactly the same thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文