TE
テックエコー
ホーム24時間トップ最新ベスト質問ショー求人
GitHubTwitter
ホーム

テックエコー

Next.jsで構築されたテクノロジーニュースプラットフォームで、グローバルなテクノロジーニュースとディスカッションを提供します。

GitHubTwitter

ホーム

ホーム最新ベスト質問ショー求人

リソース

HackerNews APIオリジナルHackerNewsNext.js

© 2025 テックエコー. すべての権利を保有。

Extending a Language – Writing Powerful Macros in Scheme

78 ポイント投稿者: textread4日前

1 comment

neilv約7時間前
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 未加载