宏用于多于1行代码 [英] Macro for more than 1 line of code

查看:80
本文介绍了宏用于多于1行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Common Lisp的宏系统,突然发现了一个问题

I'm learning the macro system of Common Lisp and suddenly found a problem

(defun hello () (format t "hello ~%")) 
(defun world () (format t "world ~%"))
(defmacro call-2-func (func1 func2)
  `(,func1)
  `(,func2))

(macroexpand-1 '(call-2-func hello world)) 
(WORLD) 
T

好吧.为什么我不能仅从一个宏生成2个LoC?我该如何解决? ( progn在更复杂的情况下将无法工作... )

Well. Why can't I generate 2 LoC from only one macro? How can I work around? (progn will not work in a more complicated situation...)

推荐答案

您的宏只需返回一个形式,即可调用这两个函数.
相反,您将生成两种形式(并且仅使用最后一种形式.)

Your macro needs to return just one form that will call both functions.
Instead you are generating two forms (and only the last one is used.)

尝试:

(defmacro call-2-func (func1 func2)
  `(progn (,func1) (,func2)))

或者如果您不想只限于两个功能:

or if you do not want to be limited to just 2 functions:

(defmacro call-funcs (&rest funcs)
  `(progn ,@(mapcar #'list funcs)))

这篇关于宏用于多于1行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆