本文介绍了LISP:如何跟踪宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但是我正在阅读PG lisp的书,我想逐步介绍他提供的带有实际值的一些示例宏,例如:

This is probably a stupid question, but I'm walking through the PG lisp book, and I wanted to step through some example macros that he provides with actual values, for instance:

(defmacro our-let (binds &body body)
    `(
        (lambda ,(
                mapcar #'(lambda (x) (if (consp x) (car x) x)) binds
            )
                ,@body
        )
        ,@(mapcar #'(lambda (x) (if (consp x) (cadr x) nil)) binds)
    )
)

我天真地尝试先运行(trace our-let),然后再运行(our-let ((x 1) (y 2)) (+ x y)),但出现错误,can't use encapsulation to trace anonymous function #<FUNCTION (MACRO-FUNCTION OUR-LET) {22675BBB}>.也不确定如何最好地将print语句放入lambda中.调试此宏/输出如何处理输入的最佳方法是什么?

I naively tried to run (trace our-let) and then (our-let ((x 1) (y 2)) (+ x y)) but I'm getting an error, can't use encapsulation to trace anonymous function #<FUNCTION (MACRO-FUNCTION OUR-LET) {22675BBB}>. Also not sure how to best put print statements into the lambdas. What's the best way to debug this macro/output how it's processing inputs?

EDIT(1):我的macroexpand格式不正确,可以正常工作.

EDIT(1): I had the incorrect formatting for macroexpand, which works.

推荐答案

调试方式:

(macroexpand-1 '(our-let ((x 1) (y 2)) (+ x y)))
; ==> ((lambda (X Y) (+ X Y)) 1 2)
; ==> t

顺便说一句,您的格式不好.这是它的外观:

BTW your formatting is not good. Here is how it can look:

(defmacro our-let (binds &body body)
  `((lambda ,(mapcar #'(lambda (x) (if (consp x) (car x) x)) binds)
      ,@body)
    ,@(mapcar #'(lambda (x) (if (consp x) (cadr x) nil)) binds)))

或者我更喜欢:

(defmacro our-let ((&rest bindings) &body body)
  (let ((names (mapcar #'(lambda (x) (if (consp x) (car x) x)) bindings))
        (exprs (mapcar #'(lambda (x) (if (consp x) (cadr x) nil)) bindings)))
    `((lambda ,names ,@body) ,@exprs)))

这篇关于LISP:如何跟踪宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 11:11