以下代碼按預期方式執行,但在最後給出NullPointerException
。我在這裏做錯了什麼?爲什麼我會在下面的代碼中獲得NPE?
(ns my-first-macro)
(defmacro exec-all [& commands]
(map (fn [c] `(println "Code: " '~c "\t=>\tResult: " ~c)) commands))
(exec-all
(cons 2 [4 5 6])
({:k 3 :m 8} :k)
(conj [4 5 \d] \e \f))
; Output:
; Clojure 1.2.0-master-SNAPSHOT
; Code: (cons 2 [4 5 6]) => Result: (2 4 5 6)
; Code: ({:k 3, :m 8} :k) => Result: 3
; Code: (conj [4 5 d] e f) => Result: [4 5 d e f]
; java.lang.NullPointerException (MyFirstMacro.clj:0)
; 1:1 user=> #<Namespace my-first-macro>
; 1:2 my-first-macro=>
(對於正確語法高亮代碼,請here)
+1 jejej,獲得使用這些括號:) – OscarRyz 2010-07-14 15:12:02
+1,任何其他方式來解決? – missingfaktor 2010-07-14 15:15:11
當然,你可以重寫它以擴大到'doseq'等,但爲什麼?這是一個完全合理的解決方案,對現有代碼的更改很少;我會說堅持下去。 – 2010-07-14 16:19:20