[racket] eginner's question on elementary textual replacement...

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Fri Mar 9 05:16:12 EST 2012

Rüdiger Asche wrote at 03/09/2012 04:39 AM:
> (let-syntax [(ONE 1)]
>    ((lambda (x) (+ ONE x))
>     2))
>
> won't work... so how do I do it?

If you really want to do this, here are two ways:

(let-syntax [(ONE (syntax-rules () ((ONE) 1)))]
   ((lambda (x) (+ (ONE) x))
    2))

(let-syntax [(ONE (syntax-id-rules () (ONE 1)))]
   ((lambda (x) (+ ONE x))
    2))

Personally, I probably wouldn't do either of these, but instead would 
simply use a variable.

And I'd almost never use "syntax-id-rules", because at least we're 
accustomed to "(id ...)" possibly being special syntax, but if we just 
see "id" outside of parenthesis and surrounding special syntax, it's 
almost always a variable.

> As a related question, what is the counterpart of C's enum as in
>
> enum
> {
>     ZERO=0,
>     ONE,
>     TWO,
>     THREE,
> <etc>
> };

Historically, people often use symbols:

(define foo-mode 'blink)

Unless you want to use a numeric value as well, in which case you could 
use variables:

(define clean-bit #b0000100)

There are some fancier things you can do, but I suggest starting with these.

BTW, I try to get people to not use all-uppercase except for syntax 
transformer pattern variables.  If you're coming from C, you might 
realize why all-uppercase for CPP macros makes tons of sense, and then 
realize that Java (which wanted to appeal to C embedded systems 
programmers) mimicked that appearance for constants even though 
constants have very little to do with CPP macros.  CPP macros can cause 
many kinds of grievous syntactic breakage and surprising bugs, and so 
all-caps as a warning is a great idea; Java constants, on the other 
hand, are one of the safest constructs.  Besides, all-caps is very 
useful in syntax transformer pattern variables, so long as you are not 
using all-caps for other purposes.

-- 
http://www.neilvandyke.org/

Posted on the users mailing list.