[plt-scheme] Dybvig's 'case' macro doesn't work?
Ken Williams wrote:
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> Good morning,
>
> I'm in the process of learning Scheme macros, and I've come across an
> issue that may indicate a bug or just ignorance on my part. Here's an
> example implementation of the case form from Dybvig's "The Scheme
> Programming Language", section 8.3:
> ;; example start
>
> (define-syntax casex
> (lambda (stx)
> (syntax-case stx ()
> [(_ e c1 c2 ...)
> (with-syntax
> ([body
> (let loop ([c1 (syntax c1)]
> [cn (syntax (c2 ...))])
Ok - c1 and cn are pieces of syntax.
> ...
> (with-syntax ([rest (loop (car cn) (cdr cn))])
Here you take car to a piece of syntax. Althoug the syntax represents
a list, it isn't a list itself. Just turn the syntax into a list of syntax
objects
using syntax->list:
(with-syntax ([rest (loop (car (syntax->list cn)) (cdr
(syntax->list cn)))])
Have you checked whether it works in PetiteChezScheme?
Hm. Strange the case - macro at
http://www.scheme.com/tspl2d/syntax.html#g2237
is different:
(define-syntax case
(lambda (x)
(syntax-case x ()
((_ e c1 c2 ...)
(with-syntax ((body
(let f ((c1 (syntax c1)) (cmore (syntax (c2 ...))))
(if (null? cmore)
(syntax-case c1 (else)
((else e1 e2 ...) (syntax (begin e1 e2 ...)))
(((k ...) e1 e2 ...)
(syntax (if (memv t '(k ...)) (begin e1 e2 ...)))))
(with-syntax ((rest (f (car cmore) (cdr cmore))))
(syntax-case c1 ()
(((k ...) e1 e2 ...)
(syntax (if (memv t '(k ...))
(begin e1 e2 ...)
rest)))))))))
(syntax (let ((t e)) body)))))))
Happy new year,
--
Jens Axel Søgaard