编程与软件工程greghendershott.com
Fear of Macros:对宏的恐惧
Fear of Macros
2014 — 2018,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这是 Greg Hendershott 撰写的 Racket 宏教程,从基础的语法变换器讲起,逐步消除程序员对 Lisp 宏的恐惧。作者循序渐进地解释宏的工作原理,被认为是最优秀的宏入门材料之一。
为什么经久不衰:它以清晰渐进的方式讲透了 Lisp 宏这一常被误解的主题,多年来多次登上 HN 首页,至今仍是学习 Racket 宏的最佳入门读物。
HN 评论精选
作者背景深厚,是知名软件 Cakewalk 的作者
Greg Hendershott 不是普通的黑客。他是 Cakewalk 的创造者——一款统治 Windows 平台约 20 年的数字音频工作站。Hendershott 是音乐创作软件领域的 John Carmack。
tomcam查看英文原文
Greg Hendersott isn’t a typical hacker. He’s the creator of Cakewalk, a digital audio workstation that ruled the Windows roost for about 20 years. Hendershott is the John Carmack of music creation software.
本文从底层原理讲起,比其他教程更易入门
在这篇文章之前,我根本不想碰 Scheme 宏。他用基础的语法变换器来介绍宏,做得非常好——其他教程都在解释原理之前就抛出 define-syntax-rule,让我屡屡失败。
cosmez查看英文原文
before this article i didn't want to touch scheme macros. he does a great job by introducing macros using basic syntax transformers, every other tutorial failed for me by introducing define-syntax-rule before explaining how it worked.
宏应克制使用,希望看到更多实际应用案例
我不害怕宏,但对其保持警惕:宏很少时,用函数替代似乎更合理,即使语法稍显凌乱;宏多到相当于发明了一门新语言时,写个 REPL 反而是更干净的分离。希望能看到一些大量而克制地使用宏的库(尤其是 Clojure 库)的例子——宏确实捕捉了'代码即数据'这一 Lisp 的核心思想,但更多解决实际问题的实例会更有帮助。
auvrw查看英文原文
i don't fear macros, but i am wary of them: when there are only a few macros, it seems as though replacing them with functions makes more sense, even if the syntax is slightly messier. when there are so many macros that you've essentially made an entirely new language, writing a REPL feels like a cleaner separation, even if it is more work. i would be interested to see links to libraries, particularly clojure libraries, that make heavy but judicious use of macros. it totally makes sense that macros are useful because they capture part of this "code as data" idea that's so central to, esp., lisps. but having more examples of actual problems that they solve would be helpful.
宏非一等公民是根本局限,Kernel 语言的一等操作符是更好的方案
我之所以对宏产生恐惧,是因为意识到宏不是一等公民——你不能把宏作为参数传给函数(只能包一层函数,而这对可变参数形式效果不佳)。J.Shutt 的 Kernel 语言中有一个更有用的概念——一等操作符(operative)。所有实体都是一等对象,可以是操作符(类似特殊形式)或应用式(Scheme 函数)。操作符不隐式求值操作数,因此可以轻松通过自定义求值模型实现类宏行为。
sparkie查看英文原文
I developed a fear of macros after realizing that they're not first-class, and you can't pass macros to functions as arguments for instance (instead you need to wrap them up in function, which doesn't work well with variadic forms). An even more useful concept exists in J.Shutt's Kernel programming language[1] - that of first class operatives. Every entity becomes a first-class object, which can be an operative (like some special forms) or an applicative (a scheme function). operatives do not implicitly evaluate the operands, so you can easily create macro-like behavior by defining your own evaluation model. It's also trivial to convert between applicatives and operatives with wrap and unwrap (which are not special forms, they're defined in terms of vau and eval). [1]:http://web.cs.wpi.edu/~jshutt/kernel.html
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选