楼主: nuomin
469 1

Lisp Macros 3 [推广有奖]

  • 8关注
  • 61粉丝

版主

院士

34%

还不是VIP/贵宾

-

TA的文库  其他...

Lisp

计量模型入门与精通

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

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

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
Can someone explain to me why Lisp macros are better than, say, the C preprocessor? (I don't know much about Lisp, and I suspect that Lisp macros are much more elegant; I just want a concrete explanation from someone who knows what he's talking about.) -- AdamSpitz
Sure. In C, the best you can do is textual substitution. This allows you do to things like
        #define MAX(x,y) ( ((x) > (y)) ? (x) : (y) )taking great care to put in all those extra parentheses. That's about the level that I, personally, can "rewrite syntax" using the C preprocessor. Note that you do not have access to C at this point, only to this mini-language provided by the preprocessor. It allows you do do things like splicing tokens in, with our without the double quotes, etc. But that's about it.
In Lisp, your macro can perform any computation on its arguments whatsoever. It can decide which arguments to evaluate, and whether they should be evaluated zero times, once, or any other number of times. If these arguments have side effects, this could be important. This features allows you to add first-class syntactic elements to the language, i.e. elements which are on par with the built-ins of the language. (In fact, many of the built-ins are macros, e.g. SETF, WITH-OPEN-FILE, etc. The number of special forms, i.e. those who have special cases for the lisp interpreter/compiler, is actually very small in lisp, and many are obscure low-level things not used very much in day-to-day programming. The obscure ones include BLOCK, GO, PROGV, SETQ. The well known ones are LET, PROGN, IF, UNWIND-PROTECT etc.
What do typical, real life macros used by lispers look like? they may look like this:
        (map-over-active-allocators (alloc :continue-on-error t)                (log-message :info "~&Notifying allocator ~A" alloc)                (my-remote-operation alloc))Which might be defined as follows:
        (defmacro map-over-active-allocators ((var &key (continue-on-error t)) &body body)         "Execute BODY over each active allocator in a context          where VAR is bound to each one in succession.          If BODY raises an error, the error is ignored and the          form is attempted over the next allocator if CONTINUE-ON-ERROR is T,          otherwise, the error is re-thrown and BODY may not be executed          on all allocators"          `(loop for allocator over (currently-active-allocators) do                (let ((,var allocator))                  (handler-case (progn ,@body)                        (error (err)                         (when ,continue-on-error (error err)))))))This code omits some issues of hygiene (e.g. continue-on-error is evaluated multiple times) for pedagogical purposes.
There - that help? :-) -- AlainPicard
Beautiful. ThankYou. :) -as
Also see CeePlusPlusTemplatesCommonLispMacrosComparison.
二维码

扫码加我 拉你入群

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

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

关键词:Macros Macro acr CRO ROS

沙发
三江鸿 发表于 2022-10-30 09:47:33 来自手机 |只看作者 |坛友微信交流群
点个赞加点人气
感谢分享

使用道具

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

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

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

GMT+8, 2024-4-20 10:48