我有以下Clojure代码,我不知道为什么它不起作用:
(defn match (x y &optional binds) (cond ((eql x y) (values binds t)) ((assoc x binds) (match (binding x binds) y binds)) ((assoc y binds) (match x (binding y binds) binds)) ((var? x) (values (cons (cons x y) binds) t)) ((var? y) (values (cons (cons y x) binds) t)) (t (when (and (consp x) (consp y)) (multiple-value-bind (b2 yes) (match (car x) (car y) binds) (and yes (match (cdr x) (cdr y) b2)))))))
(该代码由Paul Graham的ANSI Common Lisp书籍翻译而来.)
当我运行它时,我收到以下错误:
java.lang.IllegalArgumentException: Don't know how to create ISeq from: Symbol clojure.lang.Compiler$CompilerException: NO_SOURCE_FILE:2: java.lang.RuntimeException: java.lang.IllegalArgumentException: Don't know how to create ISeq from: Symbol at clojure.lang.Compiler.analyze(Compiler.java:3713) at clojure.lang.Compiler.analyzeSeq(Compiler.java:3848) at clojure.lang.Compiler.analyze(Compiler.java:3698) at clojure.lang.Compiler.access$200(Compiler.java:37) at clojure.lang.Compiler$DefExpr$Parser.parse(Compiler.java:343) at clojure.lang.Compiler.analyzeSeq(Compiler.java:3858) at clojure.lang.Compiler.analyze(Compiler.java:3698) at clojure.lang.Compiler.analyzeSeq(Compiler.java:3848) at clojure.lang.Compiler.analyze(Compiler.java:3698) at clojure.lang.Compiler.analyze(Compiler.java:3671) at clojure.lang.Compiler.eval(Compiler.java:3895) at clojure.lang.Repl.main(Repl.java:75) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at jline.ConsoleRunner.main(ConsoleRunner.java:69) Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.IllegalArgumentException: Don't know how to create ISeq from: Symbol at clojure.lang.LazyCons.rest(LazyCons.java:64) at clojure.lang.ASeq.count(ASeq.java:85) at clojure.lang.RT.count(RT.java:486) at clojure.lang.Cons.count(Cons.java:41) at clojure.lang.Compiler.analyze(Compiler.java:3695) ... 16 more Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: Don't know how to create ISeq from: Symbol at clojure.lang.LazyCons.first(LazyCons.java:44) at clojure.lang.LazyCons.rest(LazyCons.java:59) ... 20 more Caused by: java.lang.IllegalArgumentException: Don't know how to create ISeq from: Symbol at clojure.lang.RT.seqFrom(RT.java:465) at clojure.lang.RT.seq(RT.java:448) at clojure.seq__28.invoke(boot.clj:92) at clojure.every_QMARK___596.invoke(boot.clj:1180) at clojure.fn__1147$psig__1149.invoke(boot.clj:2155) at clojure.map__602$fn__605.invoke(boot.clj:1214) at clojure.lang.LazyCons.first(LazyCons.java:40) ... 21 more
我在这做错了什么?
问题是我使用括号('('和')')而不是方括号('['和']')作为参数列表.
它应该像这样开始:
(defn match [x y &optional binds]
(我在Stuart Halloway的Programming Clojure中找到了答案.)
此外,cond表单不使用parens作为其子句:
(cond test1 eval1 test2 eval2 :else eval3)
主答案已经给出了(函数参数在clojure中的[] not()中),但在尝试搞乱Common Lisp源代码之前,我建议实际学习Clojure的语法,因为它不是什么接近纯CL港口,完全没有.你只是会像处理它一样陷入困境.