我有一个包含3个函数的Clojure seq.为什么(休息my-seq)给出"不能被视为"的例外情况?

Gre*_*ams 3 debugging tail-recursion function clojure

在调试一个更大的函数的过程中,我创建了一个更简单的函数来测试错误的位置:

(defn foo [a-val p1 p2 & rest]
  (loop [curr-preds   (cons p1 (cons p2 rest))]
    (let [first-pred   (first curr-preds)
          first-bool   (first-pred a-val)
          second-bool  ((second curr-preds) a-val)
          third-bool  ((last curr-preds) a-val)]
      (println "\n\nLogical values: " first-bool second-bool third-bool)
      (println "Is it a seq?"  (seq? curr-preds))
      (if (empty? curr-preds)
        first-bool
        #_(recur (rest curr-preds))
          ))))
Run Code Online (Sandbox Code Playgroud)

p1,p2和rest中的函数集合都是谓词(例如,奇数?).我写这篇文章是为了期望它总是被调用3个谓词.

当我取出#_倒数第二行时,我收到以下错误:

java.lang.ClassCastException: clojure.lang.ArraySeq cannot be cast to clojure.lang.IFn
/Users/gr/temp/LTtemp1.clj:166 user/foo
          RestFn.java:467 clojure.lang.RestFn.invoke
Run Code Online (Sandbox Code Playgroud)

通过println陈述,我发现:

  • seq如预期的那样,curr-preds 包含3个谓词

  • 调用每个pred会a-val返回预期的结果

  • curr-preds 实际上是一个 seq

我的问题:rest定义为seqs工作,那么为什么我得到上面的不能被抛出的错误?谢谢.

ama*_*loy 5

您有一个本地命名rest,由函数参数列表绑定.你试图把 rest称为一个函数,而不是调用clojure.core/rest.

  • 每个人至少犯过一次这个错误.当你完成了十几次之后,你可能会感到尴尬. (3认同)