[r6rs-discuss] [Formal] Implementing Common Lisp's defmacro is not straigtforward.

Eli Barzilay eli at barzilay.org
Thu Sep 21 11:54:25 EDT 2006


On Sep 17, Pascal Costanza wrote:

> Full description of the issue:
> 
> Section 17.6, page 113, last paragraph, states that "Using lisp- 
> transformer, defining a basic version of Common Lisp's defmacro is a  
> straightforward exercise."
> 
> This is misleading.  [...]

I don't think so.  I read "basic version" as the basic version that is
commonly found in some Schemes -- no destructuring and no
environments.  Basically this (from an old post):

  (define macros '())
  
  (define nothing (list "nothing"))
  
  (define (preprocess-expr expr)
    (cond
     ((not (pair? expr)) expr)
     ((eq? 'defmacro (car expr))
      (set! macros
            (cons (cons (cadr expr)
                        (eval `(lambda ,@(cddr expr))))
                  macros))
      nothing)
     ((assq (car expr) macros) =>
      (lambda (m) (preprocess-expr (apply (cdr m) (cdr expr)))))
     (else (cons (preprocess-expr (car expr))
                 (preprocess-expr (cdr expr))))))
  
  (define (preprocess)
    (let loop ((expr (read)))
      (if (not (eof-object? expr))
        (let ((expr (preprocess-expr expr)))
          (if (not (eq? nothing expr))
            (begin (write expr) (newline)))
          (loop (read))))))

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!



More information about the r6rs-discuss mailing list