我已经阅读了来自 http://www.learningclojure.com/2010/09/clojure-macro-tutorial-part-i-getting.html站点的宏教程,但我不明白如何调试一些错误。有人可以描述什么是错误的/不正确的吗?
- 简单版
(defmacro dbgm_v1 [s] (list 'let ['a s] (list 'println (list 'quote s) "=" 'a) 'a) )
;;;;
(defn factorial_v1 [n] (if (< n 2) n (dbgm_v1 (* n factorial_v1(dec n)) ) ) )
;;
; let's test it
(factorial_v1 5) ClassCastException clojure.dg.alfa01$factorial_v1 cannot be cast to java.lang.Number clojure.lang.Numbers.multiply (Numbers.java:146)
- 复杂的版本
(defmacro dbgm_v9 [x] `(let [x# ~x] (println '~x "=" x#) x#) )
;
(defn factorial_v9 [n] (if (< n 2) n (dbgm_v9 (* n factorial_v9 (dec n))) ))
;
user=> (factorial_v9 5) ClassCastException clojure.dg.alfa01$factorial_v9 cannot be cast to java.lang.Nu mber clojure.lang.Numbers.multiply (Numbers.java:146) user=>
它失败并出现相同的错误。
上面的代码有什么问题?提前感谢您的任何提示/网址/注释!
危险品