Clojure中的简单符号解析错误

时间:2012-03-21 04:38:29

标签: debugging clojure

我是clojure的新手,我试图简单地生成一个带有Fibonacci序列的列表(按相反的顺序)。我尝试通过命令行运行程序(因此println显示结果):

(defn fib [n]
    (cond
     (== 1 n) '(1)
     (== 2 n) '(2 1)
     :else (fibAcc n '(2 1))))

(defn fibAcc [n lst]
    (if (zero? n)
        lst
        (fibAcc (dec n) (cons (+ (first lst) (second lst)) lst))))

(println (fib 8))

我收到以下错误:

Exception in thread "main" java.lang.RuntimeException: Unable to resolve symbol: fibAcc in this context, compiling:
C:\Users\...\ProjectEuler\2.clj:5)
        at clojure.lang.Compiler.analyze(Compiler.java:6235)
        at clojure.lang.Compiler.analyze(Compiler.java:6177)
        at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3452)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:6411)
        at clojure.lang.Compiler.analyze(Compiler.java:6216)
        at clojure.lang.Compiler.analyze(Compiler.java:6177)
        at clojure.lang.Compiler$IfExpr$Parser.parse(Compiler.java:2597)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:6409)
        at clojure.lang.Compiler.analyze(Compiler.java:6216)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:6397)
        at clojure.lang.Compiler.analyze(Compiler.java:6216)
        at clojure.lang.Compiler.analyze(Compiler.java:6177)
        at clojure.lang.Compiler$IfExpr$Parser.parse(Compiler.java:2605)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:6409)
        at clojure.lang.Compiler.analyze(Compiler.java:6216)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:6397)
        at clojure.lang.Compiler.analyze(Compiler.java:6216)
        at clojure.lang.Compiler.analyze(Compiler.java:6177)
        at clojure.lang.Compiler$IfExpr$Parser.parse(Compiler.java:2605)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:6409)
        at clojure.lang.Compiler.analyze(Compiler.java:6216)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:6397)
        at clojure.lang.Compiler.analyze(Compiler.java:6216)
        at clojure.lang.Compiler.analyze(Compiler.java:6177)
        at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:5572)
        at clojure.lang.Compiler$FnMethod.parse(Compiler.java:5008)
        at clojure.lang.Compiler$FnExpr.parse(Compiler.java:3629)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:6407)
        at clojure.lang.Compiler.analyze(Compiler.java:6216)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:6397)
        at clojure.lang.Compiler.analyze(Compiler.java:6216)
        at clojure.lang.Compiler.access$100(Compiler.java:37)
        at clojure.lang.Compiler$DefExpr$Parser.parse(Compiler.java:492)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:6409)
        at clojure.lang.Compiler.analyze(Compiler.java:6216)
        at clojure.lang.Compiler.analyze(Compiler.java:6177)
        at clojure.lang.Compiler.eval(Compiler.java:6469)
        at clojure.lang.Compiler.load(Compiler.java:6902)
        at clojure.lang.Compiler.loadFile(Compiler.java:6863)
        at clojure.main$load_script.invoke(main.clj:282)
        at clojure.main$script_opt.invoke(main.clj:342)
        at clojure.main$main.doInvoke(main.clj:426)
        at clojure.lang.RestFn.invoke(RestFn.java:436)
        at clojure.lang.Var.invoke(Var.java:409)
        at clojure.lang.AFn.applyToHelper(AFn.java:167)
        at clojure.lang.Var.applyTo(Var.java:518)
        at clojure.main.main(main.java:37)
Caused by: java.lang.RuntimeException: Unable to resolve symbol: fibAcc in this context
        at clojure.lang.Util.runtimeException(Util.java:156)
        at clojure.lang.Compiler.resolveIn(Compiler.java:6720)
        at clojure.lang.Compiler.resolve(Compiler.java:6664)
        at clojure.lang.Compiler.analyzeSymbol(Compiler.java:6625)
        at clojure.lang.Compiler.analyze(Compiler.java:6198)
        ... 46 more

2 个答案:

答案 0 :(得分:7)

fibAcc之前定义fib,或在(declare fibAcc)的定义之前添加fib

答案 1 :(得分:3)

Clojure是一个单一的'编译器'afaik,所以Jouni上面说过,你不能使用某些东西直到你定义它。

从上到下的代码顺序很重要。