アナフォリックマクロ1 - On Lisp

On Lispで、マッチングを勉強しようと思ったら acond2 にあたった。
ので勉強しますよ。

aif

まずは、間違いバージョンから。

(define-syntax aif
  (syntax-rules ()
    ((_ a b c ...)
     (let ((it a)) (if it b c ...)))))

(aif 3 (display it) (display "not 3"))

Gauche で実行すると「unbound variable: it」と怒られます。
Ruiさんに教えていただいて気づいたのですが、hygienicなマクロなので当然ですね。


なので正解は以下の通り。

(define-syntax aif
  (syntax-rules ()
    ((_ it a b c ...)
     (let ((it a)) (if it b c ...)))))
(aif it 3 (display it) (display "not 3"))

awhen

(define-syntax awhen
  (syntax-rules ()
    ((_ it pred body ...)
     (aif it pred (begin body ...)))))

実行例

(awhen it (string-append "hige" "pon")
       (display it)
       (display it))

higeponhigepon#<undef>	

awhile

(define-syntax awhile
  (syntax-rules ()
    ((_ it pred a)
     (do ((it pred pred))
         ((not it))
       a))))


aand はまた今度。