TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Extending a Language – Writing Powerful Macros in Scheme

91 点作者 textread10 天前

2 条评论

mnemenaut5 天前
<a href="https:&#x2F;&#x2F;github.com&#x2F;rogerturner&#x2F;scheme-macros&#x2F;blob&#x2F;main&#x2F;examples&#x2F;4.2%20A%20tracing%20let.ss">https:&#x2F;&#x2F;github.com&#x2F;rogerturner&#x2F;scheme-macros&#x2F;blob&#x2F;main&#x2F;examp...</a> shows stepwise development of a trace-let [the `(example: (fn arg) =&gt; result)` forms are tests - see check-examples library]
neilv7 天前
A few formatting changes might make this advanced example easier to understand:<p><pre><code> (define-syntax trace-let (syntax-rules () [(trace-let name ([var expr] ...) body1 ... body2) (let f ([depth 0] [var expr] ...) (define name (lambda (var ...) (f (+ depth 1) var ...))) (indent depth) (display &quot;(&quot;) (display &#x27;name) (begin (display &quot; &quot;) (display var)) ... (display &quot;)&quot;) (newline) (call-with-values (lambda () body1 ... body2) (lambda val* (indent depth) (fold-left (lambda (sep val) (display sep) (display val) &quot; &quot;) &quot;&quot; val*) (newline) (apply values val*))))])) </code></pre> The biggest one is to make the rule template pattern variables all-uppercase. I also made a few other tweaks, including using indentation a little more, and naming the named-`let` variable as &quot;loop&quot; (I usually name it `loop` or prefix the name with `loop-` if there&#x27;s more than one):<p><pre><code> (define-syntax trace-let (syntax-rules () ((trace-let NAME ((VAR EXPR) ...) BODY1 ... BODY2) (let loop ((depth 0) (VAR EXPR) ...) (define NAME (lambda (VAR ...) (loop (+ depth 1) VAR ...))) (indent depth) (display &quot;(&quot;) (display (quote NAME)) (begin (display &quot; &quot;) (display VAR)) ... (display &quot;)&quot;) (newline) (call-with-values (lambda () BODY1 ... BODY2) (lambda val* (indent depth) (fold-left (lambda (sep val) (display sep) (display val) &quot; &quot;) &quot;&quot; val*) (newline) (apply values val*))))))) </code></pre> Incidentally, all-uppercase Scheme pattern variables is one of the all-time best uses of all-uppercase in any language. Second only to all-uppercase for the C preprocessor, where a preprocessor macro can introduce almost arbitrary text. Using all-uppercase for constants in some language that has constants, however, is an abomination.<p>(My suspicion of why Java did all-caps is that they were developing a language for embedded systems developers who were currently using C and C++, and they wanted to make it superficially look similar, even though it was an entirely different language. And then, ironically, the language ended up being used mostly by the analogue of a very different developer of the time: corporate internal information systems developers, who, as a field, didn&#x27;t use anything like C. It&#x27;s too late to save Java, but to all other language and API developers, <i>please stop the insanity of all-caps constants, enum values, etc.</i> It&#x27;s not the most important thing that needs to jump out from the code above all other things.)
评论 #43926134 未加载