修复倒霉的宏

dav*_*ugh 0 common-lisp

经过一些试验和错误,我设法编写宏mashup来打印几个变量的名称和值(用于调试).它似乎有效,但我想看看如何更"专业"地编码?

(defmacro prt1 (var)
  ;Print a single variable and its value.
  `(progn (princ (symbol-name ',var)) (princ " = ") (princ ,var) (terpri)))

(defmacro prt (&rest vars)
  ;Print the values of a number of variable names.
  (eval `(append (list 'progn)
                 (map 'list #'(lambda (x) (list 'prt1 x)) ',vars)
                 (list (list 'terpri))
                 (list t)))) ;need to return t
Run Code Online (Sandbox Code Playgroud)

(prt A B C)然后调用打印当前绑定 - 例如:

A = 1
B = 2 
C = 3
T
Run Code Online (Sandbox Code Playgroud)

Rai*_*wig 5

(defmacro prt1 (var)
  "Print a single variable and its value."
  `(format t "~a = ~a~%" ',var ,var))

(defmacro prt (&rest vars)
  "Print the values of variables."
  `(progn ,@(loop for var in vars collect `(prt1 ,var))))
Run Code Online (Sandbox Code Playgroud)