调用递归函数的 Clojure 错误——很可能是括号问题
Clojure error calling a recursive function -- most likely a parentheses issue
我对 Clojure 还很陌生。但我正在尝试通过创建一些函数来学习语法。我在一般情况下遇到括号和语法问题...
此函数应该获取一个列表和一个位置,然后 return 删除该位置的列表——但我收到一个我不完全理解的错误。我已经阅读了一些资料,这似乎是嵌套括号的问题。但我不确定如何解决它。
如有任何反馈,我们将不胜感激。
错误:
ClassCastException java.lang.Long cannot be cast to clojure.lang.IPersistentCollection clojure.core/conj (core.clj:83)
代码:
(defn delete-at
"accepts a list and position--returns the list with
value at that position removed"
(
[L, pos]
(cond
(empty? L) nil
(zero? pos) (rest L)
:else (
delete-at (first L) (rest L) (- pos 1))
)
)
([L-new, L2, pos]
(cond
(zero? pos) (conj L-new (rest L2))
:else (
(delete-at (conj L-new (first L2)) (rest L2) (- pos 1))
)
)
)
)
让它通过 emacs 的格式化程序让我跳出这个问题:围绕最后一次 delete-at 调用的额外一组 ()
和第一个 conj 调用的参数被颠倒了。
(defn delete-at
"accepts a list and position--returns the list with
value at that position removed"
([L, pos]
(cond
(empty? L) nil
(zero? pos) (rest L)
:else (delete-at (first L) (rest L) (- pos 1))))
([L-new, L2, pos]
(cond
(zero? pos) (conj L-new (rest L2))
:else ((delete-at (conj L-new (first L2)) (rest L2) (- pos 1))))))
阅读 Clojure 时,很多堆叠式右括号 )))))
是正常的并且看起来不错(一旦您习惯了)并且堆叠式左括号 ((
会跳出可疑的地方。它主要出现在你调用一个 returns 函数的函数,然后你想调用结果函数时。
user> (defn delete-at
"accepts a list and position--returns the list with
value at that position removed"
([L, pos]
(cond
(empty? L) nil
(zero? pos) (rest L)
:else (delete-at (first L) (rest L) (- pos 1))))
([L-new, L2, pos]
(cond
(zero? pos) (conj (rest L2) L-new)
:else (delete-at (conj L-new (first L2)) (rest L2) (- pos 1)))))
#'user/delete-at
user> (delete-at [1 2 3] 1)
(1 3)
使用 cider, clojure-mode, and paredit (paredit is very useful and getting used to it is a bit of an accomplishment) This is a good place to start learning, and many people choose to use a starter kit of some sort such as emacs starter kit or emacs live
设置 emacs 非常值得
我对 Clojure 还很陌生。但我正在尝试通过创建一些函数来学习语法。我在一般情况下遇到括号和语法问题...
此函数应该获取一个列表和一个位置,然后 return 删除该位置的列表——但我收到一个我不完全理解的错误。我已经阅读了一些资料,这似乎是嵌套括号的问题。但我不确定如何解决它。
如有任何反馈,我们将不胜感激。
错误:
ClassCastException java.lang.Long cannot be cast to clojure.lang.IPersistentCollection clojure.core/conj (core.clj:83)
代码:
(defn delete-at
"accepts a list and position--returns the list with
value at that position removed"
(
[L, pos]
(cond
(empty? L) nil
(zero? pos) (rest L)
:else (
delete-at (first L) (rest L) (- pos 1))
)
)
([L-new, L2, pos]
(cond
(zero? pos) (conj L-new (rest L2))
:else (
(delete-at (conj L-new (first L2)) (rest L2) (- pos 1))
)
)
)
)
让它通过 emacs 的格式化程序让我跳出这个问题:围绕最后一次 delete-at 调用的额外一组 ()
和第一个 conj 调用的参数被颠倒了。
(defn delete-at
"accepts a list and position--returns the list with
value at that position removed"
([L, pos]
(cond
(empty? L) nil
(zero? pos) (rest L)
:else (delete-at (first L) (rest L) (- pos 1))))
([L-new, L2, pos]
(cond
(zero? pos) (conj L-new (rest L2))
:else ((delete-at (conj L-new (first L2)) (rest L2) (- pos 1))))))
阅读 Clojure 时,很多堆叠式右括号 )))))
是正常的并且看起来不错(一旦您习惯了)并且堆叠式左括号 ((
会跳出可疑的地方。它主要出现在你调用一个 returns 函数的函数,然后你想调用结果函数时。
user> (defn delete-at
"accepts a list and position--returns the list with
value at that position removed"
([L, pos]
(cond
(empty? L) nil
(zero? pos) (rest L)
:else (delete-at (first L) (rest L) (- pos 1))))
([L-new, L2, pos]
(cond
(zero? pos) (conj (rest L2) L-new)
:else (delete-at (conj L-new (first L2)) (rest L2) (- pos 1)))))
#'user/delete-at
user> (delete-at [1 2 3] 1)
(1 3)
使用 cider, clojure-mode, and paredit (paredit is very useful and getting used to it is a bit of an accomplishment) This is a good place to start learning, and many people choose to use a starter kit of some sort such as emacs starter kit or emacs live
设置 emacs 非常值得