[erlang-questions] Erlang PP Question
ok@REDACTED
ok@REDACTED
Wed May 30 13:16:25 CEST 2012
"Kannan" <vasdeveloper@REDACTED>> Thanks Richard for your response. That
makes sense.
> Actually what I wanted is to get a macro expand into two different
> functions in the source file. My intention was not producing a term. That
> is where the '.' came to be a problem.
As Richard Carlsson has explained,
Erlang source files are tokenised,
broken at ". " tokens into forms,
and only then preprocessed.
Preprocessing expands one sequence of tokens constituting
the source version of a form into another sequence of tokens
constituting the version that the parser then parses. At
this point there is no token that a macro could possibly
generated that would be interpreted as a form terminator or
separator. (Indeed, we could have an amusing debate about
whether ". " is _really_ a token... For the older among
is, is a "tape mark" a tape block?)
However, just because the built-in Erlang preprocessor cannot
do it does not mean it cannot be done by preprocessing.
For example,
define(two, `$1`'(X) -> $2`'(X, X+1). $2`'(X, Y) -> X*Y.')dnl
two(fred, nurke)
fed to m4 gives you
fred(X) -> nurke(X, X+1). nurke(X, Y) -> X*Y.
M4 takes a bit of getting used to, but it _is_ a Turing-complete
string processing language; you can do amazing things with it.
(I use M4 with Java because Java generics don't support primitive
types, and it is _amazing_ what a difference not creating a lot
of Integer and Double boxes can make to your memory usage.)
There is always Lisp-Flavoured Erlang, which puts a Lispy syntax
complete with Lispy macros on top of Erlang; I don't recall if
you can generate multiple definitions with LFE but it's
definitely one thing Lisp can do. Lisp macros turn single forms
into single forms, but a (progn Def ... Def) top level form can
contain several definitions.
More information about the erlang-questions
mailing list