[plt-scheme] Newbie macro problem

From: Anton van Straaten (anton at appsolutions.com)
Date: Fri Nov 1 18:04:05 EST 2002

> > (define (flex-format str . rest)
> >    (with-handlers ([exn:application:mismatch?
> >                     (lambda (exn)
> >                       (apply flex-format str (reverse (cdr (reverse
> > rest)))))])
> >      (apply format str rest)))
> >
> >
> > No 'count-format-arguments' required.
> >
> > And very elegant, I might add.
>
> Yikes! Take away this guy's PLT shirt!
>
> Robby
>
> PS: seriously, it seems easier to read the other version and it is
> probably much more efficient. This version does have better "single
> point of control" but I'm not sure that is relatively important given
> the loss in readability and a good test suite for flex-format.

Re readability, this example could benefit from something a bit more concise
than with-handlers, like:

(define (flex-format str . rest)
    (try-pk (apply format str rest)
            (exn:application:mismatch?
             (apply flex-format str (reverse (cdr (reverse rest)))))))

where try-pk is a limited exception handler defined as:

(define-syntax try-pk
  (syntax-rules ()
    ((try expr (pred handler))
     (with-handlers ([pred (lambda (exn) handler)])
       expr))))

"pk" stands for "takes a predicate and a naked (no lambda) handler".

Does John get his PLT shirt back now?

Anton



Posted on the users mailing list.