[plt-scheme] Dybvig's 'case' macro doesn't work?

From: Ken Williams (klw at acm.org)
Date: Mon Dec 30 09:14:06 EST 2002

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 ...)
             (let loop ([c1 (syntax c1)]
                        [cn (syntax (c2 ...))])
               (if (null? cn)
                   ; last arm of case
                   (syntax-case c1 (else)
                     [(else e1 e2 ...)
                       (begin e1 e2 ...))]
                     [((k ...) e1 e2 ...)
                       (if (memv t '(k ...))
                           (begin e1 e2 ...)))])
                   ; interior arm of case
                   (with-syntax ([rest (loop (car cn) (cdr cn))])
                     (syntax-case c1 ()
                       [((k ...) e1 e2 ...)
                         (if (memv t '(k ...))
                             (begin e1 e2 ...)
          (let ([t e]) body)))])))

(define x 17)

(casex x
         [(1 2 17) 'one]
         [(3 7 19) 'two])

;; example end

Here's what I get when I execute it:

  Welcome to DrScheme, version 203-cvs30dec2002.
  Language: Pretty Big (includes MrEd and Advanced).
   car: expects argument of type <pair>; given #<syntax:8:29>

If I place a (syntax ...) around the (car cn) and (cdr cn), then the
program loops indefinitely, consuming increasing amounts of memory.

Any suggestions?

++ Ken Williams

Posted on the users mailing list.