【问题标题】:Getting a don't know how to create an ISeq from: clojure.lang.PersistentList$1不知道如何从以下位置创建 ISeq:clojure.lang.PersistentList$1
【发布时间】:2012-01-05 12:33:14
【问题描述】:

我正在尝试实现Land of Lisp's Dice of Doom game,我得到了Don't know how to create ISeq from: clojure.lang.PersistentList$1

调用我的add-new-dice 函数时会发生这种情况:

(defn add-new-dice [board player spare-dice]
  (letfn [(f [lst n]
            (cond (empty? lst) nil
                  (zero? n) lst
                  :else (let [current-player (first (first lst))
                              current-dice (first (rest (first  lst)))]
                          (if (and (= current-player player) (< current-dice *max-dice*))
                            (cons (list current-player (+ current-dice 1))
                                  (f (rest lst) (- n 1)))
                            (cons (first lst) (f (rest list) n))))))]
    (f board spare-dice)))

用这个:

(add-new-dice '[(0 1) (1 3) (0 2) (1 1)] 0 2)

我这样做主要是为了让自己熟悉 CL 代码并获得一些将其移植到 Clojure 的经验。

如果有人能给我一些建议,将不胜感激。

【问题讨论】:

  • 花了我一段时间,但我确实让所有版本的 Dice of Doom 都与 Clojure 一起工作(尽管我使用的是 compojure 而不是家庭滚动的 Web 服务器)。如果你想看看我是怎么做的,请告诉我。不过,我的代码对于一般发布来说还不够好:(
  • 嗨,阿德里安,你以后有空聊聊吗?我在理解如何将 CL 惰性移植到惰性序列时遇到了一些困难:)...
  • 嗨,toofsideways,最好只是给我发电子邮件:我是 gmail 上的 adrian.mouat。让我知道你在哪个国家/时区。

标签: exception recursion clojure lisp


【解决方案1】:

您在倒数第二行使用函数list 而不是您的参数lst(f (rest list) n) 应该是 (f (rest lst) n)

【讨论】:

  • 感谢 Joost,这对我来说是一个严重愚蠢的错字。我疯狂地想知道我误解了其中的哪一部分。非常感谢!
  • 是的,这是 clojure 作为 Lisp-1 的潜在问题(意味着函数和“变量”/参数都占用相同的名称空间)。如果你不能为你的序列命名更有用的东西,最好使用 clojure 标准库的命名约定,并使用“coll”而不是“lst”来表示通用的可序列集合(虽然我个人更喜欢“s”作为普通序列和地图的“m”)
  • 为了我自己的启发,有人愿意解释为什么用函数替换 seq 的错字会产生给定的异常吗? clojure.lang.PersistentList 是不是list函数的数据形式?
  • 我相信这完全正确,Alex,我将函数视为列表 ((rest list))... Joost:谢谢,我想我会的!
  • @Alex:这不是 clojure.lang.PersistentList,而是 clojure.lang.PersistentList$1 - 这是 clojure.lang.PersistentList/creator 函数/方法的类。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多