请选择 进入手机版 | 继续访问电脑版
楼主: nuomin
626 2

Lisp macros 1 [推广有奖]

  • 8关注
  • 61粉丝

版主

院士

34%

还不是VIP/贵宾

-

TA的文库  其他...

Lisp

计量模型入门与精通

威望
1
论坛币
170369 个
通用积分
8028.0780
学术水平
207 点
热心指数
257 点
信用等级
151 点
经验
947 点
帖子
2452
精华
0
在线时间
4061 小时
注册时间
2005-11-15
最后登录
2023-11-28

初级热心勋章 中级热心勋章 初级信用勋章

nuomin 发表于 2019-10-6 21:27:10 |显示全部楼层 |坛友微信交流群

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
Lisp macros are very different to C macros. They are a way to transform lisp code. Macros will be used in Lisp code. During a macroexpansion phase, the Lisp expression will be passed to the macro function. This macro function can do arbitrary computation at macroexpansion time. The result of this call has to be again Lisp code. This Lisp code then is what the interpreter or compiler sees and executes or compiles.
So a typical sequence in a toplevel READ-EVAL-PRINT loop looks like this:
1.) read a Lisp expression. Input comes from a stream and the result is a Lisp expression (interned Lisp data).
(read) -> you type "(incf x)" , Lisp reads a list with two symbols.
2.) macro expand this Lisp expression if it is a macro form. the result is a new Lisp expression (interned Lisp data)
(macroexpand '(incf x)) -> some new Lisp expression
3.) compile the Lisp code
4.) execute the compiled code
5.) print the results
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.
There are few other techniques that can do what Lisp macros do, and although most of the time you don't need them, there are occasions when you really need them, and there isn't much of an alternative.
Trivial example: (defmacro eight () (+ 3 5))If I use this macro in an expression, (eight)it is equivalent to using 8 itself, because the computation is already done at compile time. However, this is easily accomplished by the C preprocessor.
Now consider the similar macro: (defmacro eight () '(+ 3 5))This expression is quoted, so now whenever I invoke (eight)it will return (+ 3 5)which is then evaluated (at run time) to yield 8. This is similar but crucially different from the first example, and this difference could not be achieved using regular functions. Think about this difference in the context of calling rand() rather than using the constant '3'.
Also the macro can return anything it wants, including fragments of programs, so it can be used to build up pieces of programs at compile time - it can generate code. It thus can be used to extend the language. This is where the real power of macros is, and it's also the most difficult part to explain.
Languages that do not support Lisp-style macros in this sense must resort to writing programs that generate programs which are run and whose results are then incorporated into yet another program, etc. I've done this many times in C, and it's a pain.
As a secondary benefit, Lisp macros can be used to do a variety of relatively less important things like add syntactic sugar, but this is trivial compared with their full power.
There are comments here about what you cannot do with them, such as that you cannot use them with apply in Common Lisp because they are not functions. That is an accident of history - Common Lisp handles functions a little strangely in general; in some dialects of Lisp, as long as the macro happens to return a function as a result, you can use the macro with apply, because apply ends up seeing a function.
The fact that you can't use macros with apply in Common Lisp and in most Scheme implementations confuses understanding of the issue, but isn't essential to the ultimate nature of macros. Many, many kinds of macros with a variety of strengths and weaknesses have been proposed and implemented, but if they don't involve doing computation at compile time, then they're not "Lisp macros".
The non-Lisp world (e.g. "macro assemblers", "C macros") usually mean something similar but slightly different with the word "macro": they still mean something that happens at "compile time" or the equivalent, but something that works by string rewriting. Interestingly, unrestricted string rewriting is Turing Complete: you can calculate any computable function by string rewriting. However, C macros, for instance, are crippled in several ways, and other more powerful macro systems like m4 and some assembly macro systems are pretty dangerous to work with precisely because they operate within the domain of strings, rather than on program representations, making Lisp macros a unique combination of power and relative safety and convenience. -- DougMerritt
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Macros Macro acr CRO ROS

tianwk 发表于 2020-2-22 23:42:35 |显示全部楼层 |坛友微信交流群
thanks for sharing

使用道具

三江鸿 发表于 2022-10-29 22:13:52 来自手机 |显示全部楼层 |坛友微信交流群
点个赞加点人气
感谢分享

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-3-29 01:09