阅读Paul Graham关于编程语言的文章,你可能会认为Lisp宏是唯一的选择。作为一个忙碌的开发人员,在其他平台上工作,我还没有使用Lisp宏的特权。作为一个想要了解热门话题的人,请解释一下是什么让这个功能如此强大。
请将这一点与我从Python、Java、c#或C开发世界中理解的东西联系起来。
阅读Paul Graham关于编程语言的文章,你可能会认为Lisp宏是唯一的选择。作为一个忙碌的开发人员,在其他平台上工作,我还没有使用Lisp宏的特权。作为一个想要了解热门话题的人,请解释一下是什么让这个功能如此强大。
请将这一点与我从Python、Java、c#或C开发世界中理解的东西联系起来。
当前回答
我从通用的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
其他回答
While the above all explains what macros are and even have cool examples, I think the key difference between a macro and a normal function is that LISP evaluates all the parameters first before calling the function. With a macro it's the reverse, LISP passes the parameters unevaluated to the macro. For example, if you pass (+ 1 2) to a function, the function will receive the value 3. If you pass this to a macro, it will receive a List( + 1 2). This can be used to do all kinds of incredibly useful stuff.
Adding a new control structure, e.g. loop or the deconstruction of a list Measure the time it takes to execute a function passed in. With a function the parameter would be evaluated before control is passed to the function. With the macro, you can splice your code between the start and stop of your stopwatch. The below has the exact same code in a macro and a function and the output is very different. Note: This is a contrived example and the implementation was chosen so that it is identical to better highlight the difference. (defmacro working-timer (b) (let ( (start (get-universal-time)) (result (eval b))) ;; not splicing here to keep stuff simple ((- (get-universal-time) start)))) (defun my-broken-timer (b) (let ( (start (get-universal-time)) (result (eval b))) ;; doesn't even need eval ((- (get-universal-time) start)))) (working-timer (sleep 10)) => 10 (broken-timer (sleep 10)) => 0
Lisp宏代表了几乎在任何大型编程项目中都会出现的一种模式。最终,在一个大的程序中,你会有一段代码,你会意识到,如果你写一个程序,把源代码输出为文本,然后你就可以粘贴进去,这会更简单,更不容易出错。
在Python中,对象有两个方法__repr__和__str__。__str__只是人类可读的表示。__repr__返回一个有效的Python代码表示,也就是说,可以作为有效的Python输入解释器。通过这种方式,您可以创建一些小的Python代码片段,生成可以粘贴到实际源代码中的有效代码。
在Lisp中,整个过程由宏系统形式化。当然,它允许您为语法创建扩展并执行各种奇特的事情,但它的实际用处可以从上面总结出来。当然,Lisp宏系统允许您使用整个语言的全部功能来操作这些“片段”是有帮助的。
简而言之,宏用于定义通用Lisp或领域特定语言(dsl)的语言语法扩展。这些语言直接嵌入到现有的Lisp代码中。现在,dsl可以具有与Lisp相似的语法(如Peter Norvig的用于通用Lisp的Prolog解释器),也可以具有完全不同的语法(例如用于Clojure的中缀符号数学)。
这里有一个更具体的例子:Python在语言中内置了列表推导式。这为常见情况提供了简单的语法。这条线
divisibleByTwo = [x for x in range(10) if x % 2 == 0]
生成一个包含0到9之间所有偶数的列表。在Python 1.5版本中还没有这样的语法;你可以使用类似这样的语句:
divisibleByTwo = []
for x in range( 10 ):
if x % 2 == 0:
divisibleByTwo.append( x )
它们在功能上是等价的。让我们暂停怀疑,假设Lisp有一个非常有限的循环宏,它只做迭代,没有简单的方法来做等价的列表推导。
在Lisp中,您可以编写如下代码。我应该指出,这个虚构的例子与Python代码相同,而不是Lisp代码的好例子。
;; the following two functions just make equivalent of Python's range function
;; you can safely ignore them unless you are running this code
(defun range-helper (x)
(if (= x 0)
(list x)
(cons x (range-helper (- x 1)))))
(defun range (x)
(reverse (range-helper (- x 1))))
;; equivalent to the python example:
;; define a variable
(defvar divisibleByTwo nil)
;; loop from 0 upto and including 9
(loop for x in (range 10)
;; test for divisibility by two
if (= (mod x 2) 0)
;; append to the list
do (setq divisibleByTwo (append divisibleByTwo (list x))))
在我进一步讨论之前,我应该更好地解释什么是宏。它是一个代码对一个代码执行的转换。也就是说,由解释器(或编译器)读取的一段代码,将代码作为参数,进行操作并返回结果,然后在适当的位置运行。
当然,这需要大量的输入,而且程序员很懒。所以我们可以定义DSL来做列表推导。事实上,我们已经在使用一个宏(循环宏)。
Lisp defines a couple of special syntax forms. The quote (') indicates the next token is a literal. The quasiquote or backtick (`) indicates the next token is a literal with escapes. Escapes are indicated by the comma operator. The literal '(1 2 3) is the equivalent of Python's [1, 2, 3]. You can assign it to another variable or use it in place. You can think of `(1 2 ,x) as the equivalent of Python's [1, 2, x] where x is a variable previously defined. This list notation is part of the magic that goes into macros. The second part is the Lisp reader which intelligently substitutes macros for code but that is best illustrated below:
因此,我们可以定义一个名为lcomp(列表理解的缩写)的宏。它的语法将完全像我们在示例中使用的python [x for x in range(10) if x % 2 == 0] - (lcomp x for x in (range 10) if (= (% x 2) 0)))
(defmacro lcomp (expression for var in list conditional conditional-test)
;; create a unique variable name for the result
(let ((result (gensym)))
;; the arguments are really code so we can substitute them
;; store nil in the unique variable name generated above
`(let ((,result nil))
;; var is a variable name
;; list is the list literal we are suppose to iterate over
(loop for ,var in ,list
;; conditional is if or unless
;; conditional-test is (= (mod x 2) 0) in our examples
,conditional ,conditional-test
;; and this is the action from the earlier lisp example
;; result = result + [x] in python
do (setq ,result (append ,result (list ,expression))))
;; return the result
,result)))
现在我们可以在命令行执行:
CL-USER> (lcomp x for x in (range 10) if (= (mod x 2) 0))
(0 2 4 6 8)
很整洁,是吧?现在还不止于此。你有一个装置,或者画笔,如果你喜欢的话。你可以使用任何你想要的语法。就像Python或c#的语法一样。或者。net的LINQ语法。最终,这就是Lisp吸引人们的地方——极致的灵活性。
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