方案宏有哪些优点?

发布于 2024-09-14 19:55:07 字数 280 浏览 6 评论 0原文

为什么有人会更喜欢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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏别ゝ 2024-09-21 19:55:07

方案宏引入了两个本质上正交的概念:卫生和模式匹配。在像 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.

触ぅ动初心 2024-09-21 19:55:07

因为它们使用不同的非 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-powerful set!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.

亣腦蒛氧 2024-09-21 19:55:07

方案宏保留引用透明度

引用《Guile参考手册》6.10.2.2卫生:

语法规则宏 (..) 保留引用透明度。当你
读取宏定义,该宏中的任何自由绑定都将被解析
相对于宏定义;当你读一个宏时
实例化时,该表达式中的所有自由绑定都被解析
相对于表达式。

此属性有时称为卫生,它确实有助于代码
清洁度。在你的宏定义中,你可以随意引入
临时变量,不用担心无意中引入
绑定到宏扩展中。

标准方案提供syntax-rulessyntax-case

Scheme macros preserve referential transparency.

Quoting the "Guile Reference Manual" 6.10.2.2 Hygiene:

syntax-rules macros (..) preserve referential transparency. When you
read a macro definition, any free bindings in that macro are resolved
relative to the macro definition; and when you read a macro
instantiation, all free bindings in that expression are resolved
relative to the expression.

This property is sometimes known as hygiene, and it does aid in code
cleanliness. In your macro definitions, you can feel free to introduce
temporary variables, without worrying about inadvertently introducing
bindings into the macro expansion.

Standard scheme offers syntax-rules and syntax-case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文