English 中文(简体)
我为什么在以下法典中拿到NPE?
原标题:Why do I get NPE in the following code?

以下编码按预期执行,但最终给出<代码>NullPointerException。 我在这里做了什么错误?

(ns my-first-macro)

(defmacro exec-all [& commands]
  (map (fn [c] `(println "Code: "  ~c "	=>	Result: " ~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=> 

http://paste.pocoo.org/show/237463/“rel=“nofollow noretinger”here

最佳回答

审视正在发生的扩大情况:

(macroexpand  (exec-all (cons 2 [4 5 6])))
=>
((clojure.core/println "Code: " (quote (cons 2 [4 5 6])) "	=>	Result: " (cons 2 [4 5 6])))

你可以看到,在你扩大后,有一对母体的外壳,这意味着克隆人试图执行印刷功能的结果,这种功能是零。

为了确定这一标准,我建议修改宏观,以包括前线的“do”,例如。

(defmacro exec-all [& commands]
  (cons  do (map (fn [c] `(println "Code: "  ~c "	=>	Result: " ~c)) commands)))
问题回答

由于《任择议定书》要求以其他可能的方式撰写这一宏观文件(见对接受的答复的评论),因此:

(defmacro exec-all [& commands]
  `(doseq [c# ~(vec (map (fn [c]
                           `(fn [] (println "Code: "  ~c "=> Result: " ~c)))
                         commands))]
     (c#)))

这一点扩大到类似的东西。

(doseq [c [(fn []
             (println "Code: "       (conj [2 3 4] 5)
                      "=> Result: " (conj [2 3 4] 5)))
           (fn []
             (println "Code: "       (+ 1 2)
                      "=> Result: " (+ 1 2)))]]
  (c))

注:fn form, 其价值将受c约束,在宏观扩展时间以病媒收集。

不用说,原来的版本比较简单,因此,我想(do......)是完美的固定体。

典型互动:

user=> (exec-all (conj [2 3 4] 5) (+ 1 2))                                                                                                    
Code:  (conj [2 3 4] 5) => Result:  [2 3 4 5]
Code:  (+ 1 2) => Result:  3
nil




相关问题
Lisp code called from Java

Long story: I am doing a project for my functional programing class, and I thought of writing an AI controller in Lisp, for the Mario AI competition. I was looking over frameworks/libraries/ways of ...

Emacs, Zen-Coding mode, and Putty

I use emacs via Putty and since Putty doesn t send certain key combinations to the remote console I generally need to re-bind them to other key combinations. After installing the amazing Zen-Coding ...

In Which Cases Is Better To Use Clojure? [closed]

I develop in Lisp and in Scheme, but I was reading about Clojure and then I want to know, in which cases is better to use it than using Lisp or Scheme? Thanks

lambda-gtk negative pointer

I was trying to write my own put-pixel on (Gdk) pixbuf in Lisp. When I finally realized how I can operate on C pointers in CL, new obstacle came along - (gdk:pixbuf-get-pixels pb) returns me negative ...

Is there a common lisp package naming convention?

I have created some of my own user packages and have run into a name clash. In Java, the naming convention is to use your domain name in the package name: e.g. import com.example.somepackage;. Are ...

SOAP request from within an AutoLISP/AutoCAD macro

We have built a webservice for a client that uses AutoCAD. They have a macro that runs in AutoCAD that builds a SOAP request. But they have not figured out how to actually send() the soap request to ...

热门标签