[plt-scheme] Recognizing datums in macros

From: Carl Eastlund (carl.eastlund at gmail.com)
Date: Tue Feb 7 19:28:11 EST 2006

On 2/7/06, Jens Axel Søgaard <jensaxel at soegaard.net> wrote:
> Consider:
>
>    (define-syntax (define-constant-setter stx)
>      (syntax-case stx ()
>        [(define-constant-setter name! datum)
>         #'(define-syntax (name! stx) ()
>             (syntax-case stx ()
>               [(name! var)
>                #'(begin (set! var datum)
>                         datum)]))]))
>
>    (define-constant-setter null!            '())
>    (define-constant-setter zero!            0)
>    (define-constant-setter one!             1)
>    (define-constant-setter infinite!        +inf.0)
>    (define-constant-setter minus-infinite!  -inf.0)
>    (define-constant-setter empty-vector!    #())
>
> With the intended usage:
>
>    > (define foo 42)
>    > (null! foo)
>    ()
>    > foo
>    ()
>
> Is there a way to signal an error if datum is not a datum?

Out of curiosity, why is it necessary for datum to be a datum?  I
understand you don't want to reevaluate an expression over and over at
each use of name!, but couldn't you throw the value of an expression
into a new variable once and retrieve it from there each time?

--
Carl Eastlund
"Cynical, but technically correct."


Posted on the users mailing list.