[racket] eginner's question on elementary textual replacement...
On Fri, 2012-03-09 at 10:39 +0100, Rüdiger Asche wrote:
> [...]
> I need a counterpart for the C #define statement, meaning a way to
> textually replace every instance of id at preprocessor time with its
> defined value, as in
> [...]
Hello,
in most cases you should probably simply use a regular definition like
this:
(define ONE 1)
If that definition is not exported from the module you are writing it
will probably be inlined anyway.
If you really want a syntactic replacement, syntax-id-rules could be
what you are looking for:
(define-syntax ONE (syntax-id-rules () [ONE 1]))
> [...]
> As a related question, what is the counterpart of C's enum
> [...]
I would say that there is no canonical counterpart to C enumerations in
Scheme. You can either
- simply use symbols, which work just fine for switching on them
using case forms or comparing them efficiently using eq?
- use R6RS enumerations [1], but beware of pitfalls concerning the
conversion procedures associated with these beasts, since R6RS
has a different notion of lists than Racket does.
- use define-datatype from EOPL [2] or HTDP [3] with nullary variant
constructors
- manually construct nullary structure types and instances
The latter two alternatives make more sense, of course, if only some of
the alternatives you want to represent have no arguments and others are
really linked to additional data, ie. if the thing you are trying to
port from C is in fact a discriminated union type rather than just an
enumeration standing alone.
I hope this helps :-)
Ciao,
Thomas
--
[1] http://docs.racket-lang.org/r6rs-lib-std/r6rs-lib-Z-H-15.html
[2] http://docs.racket-lang.org/eopl/index.html#(form._((lib._eopl/eopl..rkt)._define-datatype))
[3] http://docs.racket-lang.org/htdp-langs/advanced.html#(form._((lib._lang/htdp-advanced..rkt)._define-datatype))