Perpetual Weekend

1 December 2006

Quasiquote redux

Filed under: Uncategorized — shaurz @ 0:02

I have updated my quasiquote macro to support unquote-splicing.

(define (expand-quasiquote x)
  (if (pair? x)
      (if (is? (car x) 'unquote)
          (cadr x)
      (if (is? (car x) 'unquote-splicing)
          (error "unquote-splicing not inside list")
      (if (and (pair? (car x)) (is? (caar x) 'unquote-splicing))
          (list 'append
                (cadar x)
                (expand-quasiquote (cdr x)))
          (list 'cons
                (expand-quasiquote (car x))
                (expand-quasiquote (cdr x))))))
      (list 'quote x)))

(define-macro (quasiquote x) (expand-quasiquote x))

(define-macro (unquote x)
  (error "unquote not inside quasiquote"))

(define-macro (unquote-splicing x)
  (error "unquote-splicing not inside quasiquote"))

On another note, I have discovered a wonderful beer called “Innis & Gunn”. It is aged in oak whiskey barrells and has a whiskey-like taste, yet it is quite light. Not a beer to gulp down though, sip slowly to really enjoy it. I’m beginning to sound like an advert!

It occurred to me that macro-expansion has at least two possible semantics: expand the arguments of a macro first, or not. I have currently implemented the latter since it seems more natural to me for a macro to be passed the expressions as-typed, but I am not sure if this is “correct”. I must investigate.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.