「letはlambdaのシンタックスシュガー」という話を聞いたので。
letのマクロによる実装 (OnLispのコードより)
(defmacro our-let (binds &body body)
`((lambda ,(mapcar #'(lambda (x)
(if (consp x) (first x) x))
binds)
,@body)
,@(mapcar #'(lambda (x)
(if (consp x) (second x) nil))
binds)))
(macroexpand '(our-let ((x 1) (y 2)) (+ x y)))
=> ((lambda (x y) (+ x y)) 1 2)
スペシャル変数も束縛可能
(list (our-let ((*features* '(:hoge1 :hoge2 :hoge3)))
(member :hoge2 *features*))
(member :hoge2 *features*))
=> ((:hoge2 :hoge3) nil)
0 件のコメント:
コメントを投稿