マクロは、Lispが1つのプログラミング言語として有する唯一最大の強みであり、いかなるプログラミング言語においても唯一最大の強みである。 --Doug Hoyte著者のLisp(のマクロ)マンセーっぷりがすごい。
(Gaucheで書いてみてます)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; nlet | |
(define-macro (nlet n letargs . body) | |
`(letrec ((,n (lambda ,(map car letargs) | |
,@body))) | |
(,n ,@(map cadr letargs)))) | |
(define (fact n) | |
(nlet loop ((n n)) | |
(if (zero? n) | |
1 | |
(* n (loop (- n 1)))))) | |
(fact 5) | |
; -> 120 | |
(let loop () | |
(display 1)) | |
; -> 1#<undef> | |
(define-syntax nlet | |
(syntax-rules () | |
((_ name ((var val) ...) body ...) | |
(letrec ((name (lambda (var ...) | |
body ...))) | |
(name val ...))))) | |
(define (fact n) | |
(nlet loop ((n n)) | |
(if (zero? n) | |
1 | |
(* n (loop (- n 1)))))) | |
(fact 5) | |
; -> 120 | |
Gauche 限定でならマクロを使わずに nlet を定義できちゃったり。
返信削除(define nlet let)
named-letってことですよね。
返信削除LOLを読みながらGaucheで写経したりしようかなーと(笑)
すいません。 言葉が足りませんでした。 練習のための写経なのはわかっているので scheme の named-let のことを言いたかったのではないです。
返信削除特殊構文やマクロはファーストクラスのオブジェクトでは無いので普通の Scheme では define で別名を付けることは出来ないのですが、 Gauche では出来てしまうというちょっと変なところを紹介してみた次第です。 (実際、他に (define nlet let) が出来る Scheme 処理系を見付けることは出来ませんでした。)
なるほどー!そういうことだったんですか!すいませんでした^^;
返信削除>特殊構文やマクロはファーストクラスのオブジェクトでは無いので普通の Scheme では define で別名を付けることは出来ないのですが、 Gauche では出来てしまうというちょっと変なところを紹介してみた次第です。 (実際、他に (define nlet let) が出来る Scheme 処理系を見付けることは出来ませんでした。)
そうだったんですか。評価すればなんでも値が返ってきていたので、それが普通なのかと思っていました。。
なるほど、それがわかると(define nlet let)がすごくおもしろいですね(笑)勉強になります。ありがとうございました。