方案宏有哪些优点?
为什么有人会更喜欢Scheme宏而不是Common Lisp宏(我也真的很想知道,我并不是想成为一个巨魔)?
作为一个 Lisp 新手,我的经验是 Common Lisp 风格的宏比 Scheme 的宏更容易学习。我还没有看到Scheme 宏的任何优点,但这当然并不意味着它们不存在。
我确实知道方案宏是“卫生的”,但我仍然不相信这值得额外的复杂性。但另一方面,显然有人相信这是必要的,否则 Common Lisp 中就不会实现Scheme 宏。
长话短说,有人可以为我辩护一下Scheme的宏吗?
Why would anyone prefer Scheme macros over Common Lisp macros (and I genuinely want to know too, I'm not trying to be a troll)?
My experience as a Lisp newb is that Common Lisp style macros are much easier to learn than Scheme's macros. I have yet to see any advantages to Scheme's macros, but of course that doesn't mean they don't exist.
I do know that Scheme macros are "hygenic", but I'm still not convinced this is worth the additional complexity. On the other hand though, there obviously are people that are convinced that this is necessary, otherwise there wouldn't be implementations of Scheme macros in Common Lisp.
To make a long story short, can someone defend Scheme's macros to me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
方案宏引入了两个本质上正交的概念:卫生和模式匹配。在像 Common Lisp 这样的 lisp2 中,卫生性不太重要。模式匹配语言捕获了许多常见的宏习惯用法,但存在的问题是它本质上是与方案不同的语言。 Shriram Krishnamurthi 的 PLAI 第 36 章和第 37 章。
我怀疑人们在 common lisp 中编写方案样式宏系统的原因更多是为了模式匹配而不是为了卫生。
Scheme macros introduce two, essentially orthogonal, concepts: hygiene and pattern matching. Hygiene is less important in a lisp2 like Common Lisp. The pattern matching language captures many of the common macro idioms, but has the problem that it is essentially a different language from scheme. Probably the best introduction to scheme's macros, along with some of the rationale behind them is Shriram Krishnamurthi's PLAI chapters 36 and 37.
I suspect that the reason people write scheme style macro systems in common lisp is more for the pattern matching than for the hygiene.
因为它们使用不同的非 Scheme 语言,所以从几乎正式的意义上来说,Scheme 宏的功能不如 Common Lisp 宏:您可以用它们进行任意编译时计算,但它非常繁琐且复杂。这很像不使用
set!
的争论:功能较弱的set!
自由语言会产生更少的错误代码,以换取笨拙的状态处理。任何时候你用权力换取纪律,从长远来看,你都在押注自己将能够构建更复杂的系统。这是我见过的关于Scheme宏相对于Common Lisp宏的最佳论点:如果你在Scheme之上构建一种复杂的语言,如果你坚持使用标准宏系统,那么你就不太可能引入微妙的宏错误。
就我个人而言,我不使用宏构建大型语言,所以我更喜欢 Common Lisp 宏。我发现它们对于小型工作来说更容易,并且避免变量捕获等在小规模上并不是什么大问题。
Because they use a different, non-Scheme language, Scheme macros are less powerful than Common Lisp macros in the almost-formal sense: you can do arbitrary compile-time computation with them, but it's hairy and convoluted. It's a lot like the argument for not using
set!
: less-powerfulset!
free languages produces less buggy code in exchange for awkward handling of state. Any time you trade power for discipline, you are betting that you will be able to build more complex systems in the long run.That's the best argument I've seen for Scheme macros over Common Lisp ones: if you are building a complex language on top of Scheme, you are less likely to introduce subtle macro bugs if you stick with the standard macro system.
Personally, I don't build big languages using macros, so I prefer Common Lisp macros. I find them much easier for small jobs and avoiding variable capture etc isn't a big deal on a small scale.
方案宏保留引用透明度。
引用《Guile参考手册》6.10.2.2卫生:
标准方案提供
syntax-rules
和syntax-case
。Scheme macros preserve referential transparency.
Quoting the "Guile Reference Manual" 6.10.2.2 Hygiene:
Standard scheme offers
syntax-rules
andsyntax-case
.