[plt-scheme] match single symbol

From: Carl Eastlund (carl.eastlund at gmail.com)
Date: Thu Sep 3 12:03:17 EDT 2009

On Thu, Sep 3, 2009 at 11:58 AM, Jon Rafkind<rafkind at cs.utah.edu> wrote:
> I need to match any single symbol and got momentarily confused about the
> literal patterns. I thought there was a pattern for matching any string, but
> its just a literal string.
> This is essentially what I need:
> (define-match-expander symbol (lambda (stx) #'(? symbol?)))
> (match 'x
>      [symbol 'ok]
>      [else 'bad])

Bear in mind you will have to use (symbol), not symbol, to refer to
that pattern.  If you just use an unadorned identifier, match will
treat it as a variable, treat it as a match against any value, and
bind it to that value in the right hand side.  All custom patterns
must have the form of macro applications with parentheses.

Honestly, for this case, you're probably best off just using (?
symbol?) for uniformity with every other match-using program out
there.  It's only 3 extra characters beyond (symbol).  But that's just
my opinion.


Posted on the users mailing list.