阅读Paul Graham关于编程语言的文章,你可能会认为Lisp宏是唯一的选择。作为一个忙碌的开发人员,在其他平台上工作,我还没有使用Lisp宏的特权。作为一个想要了解热门话题的人,请解释一下是什么让这个功能如此强大。
请将这一点与我从Python、Java、c#或C开发世界中理解的东西联系起来。
阅读Paul Graham关于编程语言的文章,你可能会认为Lisp宏是唯一的选择。作为一个忙碌的开发人员,在其他平台上工作,我还没有使用Lisp宏的特权。作为一个想要了解热门话题的人,请解释一下是什么让这个功能如此强大。
请将这一点与我从Python、Java、c#或C开发世界中理解的东西联系起来。
当前回答
In python you have decorators, you basically have a function that takes another function as input. You can do what ever you want: call the function, do something else, wrap the function call in a resource acquire release, etc. but you don't get to peek inside that function. Say we wanted to make it more powerful, say your decorator received the code of the function as a list then you could not only execute the function as is but you can now execute parts of it, reorder lines of the function etc.
其他回答
In python you have decorators, you basically have a function that takes another function as input. You can do what ever you want: call the function, do something else, wrap the function call in a resource acquire release, etc. but you don't get to peek inside that function. Say we wanted to make it more powerful, say your decorator received the code of the function as a list then you could not only execute the function as is but you can now execute parts of it, reorder lines of the function etc.
lisp宏以程序片段作为输入。这个程序片段被表示为一个数据结构,可以按照您喜欢的任何方式进行操作和转换。最后,宏输出另一个程序片段,这个片段是在运行时执行的。
c#没有宏功能,但是如果编译器将代码解析为CodeDOM树,并将其传递给一个方法,该方法将其转换为另一个CodeDOM,然后将其编译为IL,则会有等效的宏功能。
这可以用来实现“糖”语法,如for each-statement using-clause, linq select-expressions等等,作为转换为底层代码的宏。
如果Java有宏,您就可以在Java中实现Linq语法,而不需要Sun更改基本语言。
下面是在c#中实现lisp风格的宏的伪代码:
define macro "using":
using ($type $varname = $expression) $block
into:
$type $varname;
try {
$varname = $expression;
$block;
} finally {
$varname.Dispose();
}
我从通用的lisp烹饪书中得到了这个,我认为它解释了为什么lisp宏是有用的。
宏是一段普通的Lisp代码,它对另一段假定的Lisp代码进行操作,将其翻译成(更接近于)可执行的Lisp。这听起来可能有点复杂,所以让我们举一个简单的例子。假设您想要一个版本的setq,将两个变量设置为相同的值。所以如果你写
(setq2 x y (+ z 3))
当z=8时,x和y都被设为11。(我想不出这有什么用,但这只是一个例子。)
It should be obvious that we can't define setq2 as a function. If x=50 and y=-5, this function would receive the values 50, -5, and 11; it would have no knowledge of what variables were supposed to be set. What we really want to say is, When you (the Lisp system) see (setq2 v1 v2 e), treat it as equivalent to (progn (setq v1 e) (setq v2 e)). Actually, this isn't quite right, but it will do for now. A macro allows us to do precisely this, by specifying a program for transforming the input pattern (setq2 v1 v2 e)" into the output pattern (progn ...)."
如果你觉得这很好,你可以继续读下去: http://cl-cookbook.sourceforge.net/macros.html
通用Lisp宏本质上扩展了代码的“语法原语”。
例如,在C语言中,switch/case结构只适用于整型,如果你想将它用于浮点数或字符串,你就只能使用嵌套的if语句和显式比较。你也不可能编写一个C宏来为你做这项工作。
但是,由于lisp宏(本质上)是一个lisp程序,它接受代码片段作为输入,并返回代码来替换宏的“调用”,因此您可以尽可能地扩展您的“原语”库,通常最终会得到一个更可读的程序。
要在C中做同样的事情,您必须编写一个自定义预处理器,它会吃掉您的初始(不完全是C)源代码,并吐出C编译器可以理解的东西。这不是一种错误的方法,但它不一定是最简单的。
想想在C或c++中可以用宏和模板做什么。它们是管理重复代码的非常有用的工具,但它们在相当严重的方面受到限制。
有限的宏/模板语法限制了它们的使用。例如,不能编写扩展为类或函数以外内容的模板。宏和模板不容易维护内部数据。 C和c++复杂且不规则的语法使得编写非常通用的宏非常困难。
Lisp和Lisp宏解决了这些问题。
Lisp宏是用Lisp编写的。您拥有Lisp的全部功能来编写宏。 Lisp有一个非常规则的语法。
与任何精通c++的人交谈,问他们花了多长时间来学习模板元编程所需的所有模板。或者是《现代c++设计》等(优秀)书籍中的所有疯狂技巧,尽管语言已经标准化了10年,但这些技巧仍然很难调试,而且(在实践中)无法在真实的编译器之间移植。如果用于元编程的语言与用于编程的语言相同,那么所有这些问题都消失了!