[racket] a little macro exercise

From: Eli Barzilay (eli at barzilay.org)
Date: Fri Oct 8 22:57:36 EDT 2010

Here's a much shorter version that doesn't require goto emulation, and
is also almost syntax-rules-kosher, modulo a syntax parameter for
breaking:

-------------------------------------------------------------------------------
#lang racket

(provide cas-cad-e)

(require racket/stxparam)

(define-syntax-parameter break
  (lambda (stx)
    (raise-syntax-error #f "break must be used inside a cas-cad-e")))

(define-syntax helper
  (syntax-rules ()
    [(_ v*s tail val [(v0 ...) e0 ...] [v e ...] ...)
     (helper (v0 ... . v*s) (when (memq val '(v0 ... . v*s)) tail e0 ...)
             val [v e ...] ...)]
    [(_ _ tail val) tail]))

(define-syntax-rule (cas-cad-e expr [v e ...] ...)
  (let ([val expr])
    (let/ec escape
      (syntax-parameterize ([break (make-rename-transformer #'escape)])
        (helper () (void) val [v e ...] ...)))))
-------------------------------------------------------------------------------

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


Posted on the users mailing list.