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

From: Rüdiger Asche (rac at ruediger-asche.de)
Date: Fri Mar 9 05:36:36 EST 2012

Thanks, Neil -

of course I could always use variables - one could do the same thing  
in C, using

const unsigned char ONE = 1;

The reason why one doesn't do it in C is that it costs you useless  
storage, execution time overhead (the compiler can generally generate  
more efficient code assigning constant values instead of reading form  
the constant location first before storing back) and readability.

I am under the impression that bindings under Scheme are generally  
more costly as well, so that's why I shy away from using bound  
variables and would rather have some kind of super simple 1:1  
syntactic replacement without bells and whistles.

The let-syntax road is definitly out of question because there will  
typically be dozens or more of declarations, and dedicating a  
expression such as

(ONE (syntax-id-rules () (ONE 1)))

for each and every one simply obfuscates what's behind it which is  
exactly the opposite of the intention - probably one could write yet  
another syntax rule that expands into the above expression, but to me  
it seems a little strange to write nested layers of syntax rules if  
all one wants is a simple textual replacement of every occurrence of  
ONE by 1 before evaluation time?...

Thanks again!

Quoting Neil Van Dyke <neil at neilvandyke.org>:

> 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.