`
daibalusu
  • 浏览: 341841 次
文章分类
社区版块
存档分类
最新评论

Macro in LISP (LISP中宏的要义)

 
阅读更多

文章片段来自:

Practical Common LispSyntax and Semantics


The evaluation of a macro form proceeds in two phases: First, the elements of the macro form are passed, unevaluated, to the macro function. Second, the form returned by the macro function--called its-expansion--is evaluated according to the normal evaluation rules.
It's important to keep the two phases of evaluating a macro form clear in your mind. It's easy to lose track when you're typing expressions at the REPL because the two phases happen one after another and the value of the second phase is immediately returned. But when Lisp code is compiled, the two phases happen at completely different times, so it's important to keep clear what's happening when. For instance, when you compile a whole file of source code with the functionCOMPILE-FILE, all the macro forms in the file are recursively expanded until the code consists of nothing but function call forms and special forms. This macroless code is then compiled into a FASL file that theLOADfunction knows how to load. The compiled code, however, isn't executed until the file is loaded. Because macros generate their expansion at compile time, they can do relatively large amounts of work generating their expansion without having to pay for it when the file is loaded or the functions defined in the file are called.
For now the important thing for you to realize is that macros--while syntactically similar to function calls--serve quite a different purpose, providing a hook into the compiler.


计算一个宏表格分为两步处理:第一步,宏表格中的元素被原样不动的传递给宏函数(即宏)。第二步,宏函数返回表格--S表达式,被以正常的方式计算其结果。


你应该牢记计算一个宏要分两步。这很容易搞混,你在REPL打表达式时,因为两个阶段是连接在一起的,第二阶段的值就直接返回了。但是当Lisp代码被编译时,两个阶段就在完全不同的两个时间,所以有必要搞清楚什么时候会发生什么。举个例子,当你用COMPILE-FILE编译一段源码时,文件中所有的宏都会被递归展开,直到只剩下函数和特殊函数。这些无宏代码被编译成FASL文件(fast-load file),被LOAD 函数加载。然而,编译后的代码在加载前是不会执行的。所以,宏在编译时展开,他们能在编译时做大量的扩展工作,而不用在加载或者函数调用时花时间。(译者:这是宏之所以称之为宏的原因。)


现在你要关注的是:宏虽然在语法上很像函数调用,但却有着截然不同的目的:为编译时提供一个连接器。


分享到:
评论

相关推荐

    Programming Macros in Lisp

    Crucially, the macro language is Lisp itself, so the full power of the language is available. Contrast this with C macros, where a separate language (which doesn't know how C works) is used.

    C/C++中宏/Macro的深入讲解

    宏(Macro)本质上就是代码片段,通过别名来使用。在编译前的预处理中,宏会被替换为真实所指代的代码片段,即下图中 Preprocessor 处理的部分。 C/C++ 代码编译过程 – 图片来自 ntu.edu.sg 根据用法的不同,分两...

    SAS macro language magic

    sas macro language in PDF format

    babel-plugin-as-macro:JavaScript代码中宏的功能

    babel-plugin-as-macro JavaScript代码中宏的功能。动机当我使用,我想使用模块进行国际化。 但是使用文件系统( fs模块)保存单词及其含义。 禁止导入使用文件系统的任何模块。 我发现的第一个解决方案是模块。 ...

    Macro F1 and Macro F1.pdf

    Macro F1 and Macro F1是2019年的一篇关于两种宏观F1-score的计算方式的详细讲解。

    excel macro

    好用 的 excel macro,可以build好用 的 excel macro,可以build好用 的 excel macro,可以build好用 的 excel macro,可以build

    Macro-Awareness in Relative Value Trading.pdf

    Macro-Awareness in Relative Value Trading.pdf

    FreeMarker使用Macro实例

    FreeMarker使用Macro实例,很好的学习资料。

    清理Macro1残余提示

    清理Macro1残余提示 Macro1Macro1Macro1

    Macro- and Micro-Expression Spotting in Long Videos Using Spatio-temporal Strain

    宏微观表情识别论文,这是一篇英语原文,不是翻译成品文,注意注意!

    [confluence插件] confluence-markdown-macro-1.6.22.jar

    [confluence插件] confluence-markdown-macro-1.6.22.jar [confluence插件] confluence-markdown-macro-1.6.22.jar [confluence插件] confluence-markdown-macro-1.6.22.jar [confluence插件] confluence-markdown-...

    CRLF------MACRO.rar_CRLF MACRO_微机crlf macro

    设有10个学生的成绩分别是76,69,84,90,73,88,89,63,100,80分,试编制一个子程序统计60~69分,70~79分,80~89分,90~99分及100分的人数,放在S6,S7,S8,S9和S10单元中

    Macro文件制作方法

    Macro工具(或图标或快捷键)制作完成,建议将自己习惯的一些操作作成自定义工具,以提高工作效率。但是请注意,设置快捷键时,不要和默认的快捷键干涉,也不要去修改默认的快捷键。这样有利于其他同事使用该电脑。 ...

    macro-compat_2.12-1.1.1-API文档-中文版.zip

    赠送jar包:macro-compat_2.12-1.1.1.jar; 赠送原API文档:macro-compat_2.12-1.1.1-javadoc.jar; 赠送源代码:macro-compat_2.12-1.1.1-sources.jar; 赠送Maven依赖信息文件:macro-compat_2.12-1.1.1.pom; ...

    解析macro.ini

    用于解析macro.ini并比较新旧的macro.ini文件,输出差异文档。

    Macro Recorder v5.7.1注册版.rar

    JitBit Macro Recorder是一款用于记录键盘鼠标动作的小工具,虽然它的体积很小但是功能却不容小看,它能够轻松记录下所有的键盘和鼠标动作,录制之后可以进行编辑以及回放,你也可以制作成EXE格式的文件发送给朋友...

    Laravel开发-laravel-blade-macro

    Laravel开发-laravel-blade-macro laravel blade`@macro`指令。

    macro-compat_2.12-1.1.1-API文档-中英对照版.zip

    赠送jar包:macro-compat_2.12-1.1.1.jar; 赠送原API文档:macro-compat_2.12-1.1.1-javadoc.jar; 赠送源代码:macro-compat_2.12-1.1.1-sources.jar; 赠送Maven依赖信息文件:macro-compat_2.12-1.1.1.pom; ...

Global site tag (gtag.js) - Google Analytics