楼主: nuomin
820 1

Lisp Macros 4 [推广有奖]

  • 8关注
  • 61粉丝

版主

已卖:573份资源

院士

34%

还不是VIP/贵宾

-

TA的文库  其他...

Lisp

计量模型入门与精通

威望
1
论坛币
170716 个
通用积分
8029.6016
学术水平
207 点
热心指数
257 点
信用等级
151 点
经验
878 点
帖子
2429
精华
0
在线时间
4069 小时
注册时间
2005-11-15
最后登录
2025-11-15

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

楼主
nuomin 发表于 2019-10-6 21:35:32 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
I've been thinking a lot about macros ever since I asked my question here last year. I think I understand them a lot better now, and part of my brain recognizes them as a really elegant idea - but I'm still having trouble coming up with situations in which I'd want to use them.
I should probably explain where I'm coming from. I'm a Smalltalk/Ruby guy, and I'm trying to imagine what Ruby-with-macros would look like. And I can do that - I can imagine a language that looks like Ruby, has Ruby's object system, and has Lisp-like macros underneath - but I'm having trouble coming up with examples where macros are really a big win. In a few cases, macros seem like they might streamline a bit of syntax (usually by allowing me to write a "naked" Symbol), but... I dunno. It seems like macros ought to be useful for more than that.
After thinking about them a bit, none of the examples on this page are particularly compelling - they all seem to me like workarounds for the ugliness of typing "lambda". But Smalltalk and Ruby both have a much more lightweight syntax for closures. I can write an equivalent to your with-scarce-resource macro using just functions and closures:
        def with_scarce_resource(&body) {          resource = allocator_for_my_scarce_resource()          try {                body.call(resource)          } finally {                deallocate_my_scarce_resource(resource)          }        }(That's not valid Ruby code; it's just the syntax that I'm imagining for this new language. Imagine that the try__finally function is the equivalent of your unwind-protect - it's a function that takes two closures, starts executing the first, and executes the second when the first stops for whatever reason.)
So now I can write:
  with-scarce-resource { |r|        foo()        bar()        etc()  }Point is, I didn't need macros to do what you did in the Lisp version, and you didn't need them either - you could have done everything with normal functions and lambdas, except that "lambda" is ugly to type, so you wrapped it in a macro. And it seems to me that the map-over-active-allocators example above is similar.
So my current theory is that lambdas are so fundamental that they deserve a special lightweight syntax, and that once you have that, you don't need macros so much anymore. But there must be other things that you use macros for. What are they?
-- AdamSpitz
二维码

扫码加我 拉你入群

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

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

关键词:Macros Macro acr CRO ROS

沙发
三江鸿 发表于 2022-10-30 09:47:04 来自手机
点个赞加点人气
感谢分享

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

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2026-1-3 21:37