[REBOL] Re: lisp-like backquotes macros
From: volker:nitsch:gmai:l at: 26-Nov-2005 18:59
On 11/26/05, Henri Morlaye <henri.morlaye-gmail.com> wrote:
> > Probably Ladislav's BUILD will help; anyway, sometimes it is
> > possible to refactor your code so that you don't need it.
> > In the case above, for example, you could write:
> > compose [(value1) to-string 2 + (to-integer value2)]
> > (Well, actually, in this specific case you could just write:
> > reduce [value1 to-string 2 + to-integer value2]
> > because there's no reason to do the add and the string conversion
> > at "runtime", but I assume it's just because this is a simplified
> > example.)
> I don't want it to be done at runtilme. The value is in fact the name
> of a variable, I create a standard block for a standard function, and
> then I create several functions using different variables by
> replicating/compositing the standard block.
> > Also, REBOL offers another way to obtain the same goal without
> > composing at all.
> > use [value1' value2'] [
> > value1': value1
> > value2': to-integer value2
> > [value1' to-string value2' + 2]
> > ]
> > Using a function like my LOCALIZE in:
> > http://www.colellachiara.com/soft/Libs/utility.r
> > localize [value1 value2] [
> > [value1 to-string 2 + to-integer value2]
> > ]
> > The solutions here do not solve all the cases, especially when you
> > are composing PARSE rules; but they can give you ideas.
> Thank you very much, utility.r and build.r are nice, I will probably
> find a solution with them, nevertheless they all seem like workarounds
> for the lack of backquotes macros, I still don't understand why
> RebolTech decided to use parens for compose. The lispy method seems so
> much more comfortable, and more accurate with the use of "-" only when
> needed instead of the "/only".
Deep lisp-magic? :)
RT does not support backquotes IMHO because in rebol we rarely do it that deep.
Rebol has other ways, contexts and parse-rules (dialects).
I for example use Compose only for small parts (IMHO else /deep would
be the default ;)
`[ ,value1 to-string ( to-integer ,value2 ) + 2 ) ]
You want a lot of functions like that. functions that look like
f: func[ value1 to-string ( to-integer value2 ) + 2 ]
Done like this:
ctx: context [
value1: value2: none
f: func  [value1 to-string (to-integer value2) + 2]
o1: make ctx [value1: "v1" value2: "1"]
o2: make ctx [value1: "v2" value2: "22"]
o1/f ; == "3"
o2/f ; == "24"
Has an advantage in rebol, because we have side-effects here. The
values can be changed after the closure is bound, so inlining them
makes less sense.
So i put dynamic values in the context and use unmodified clones of
the template-functions. Not like lisp, where you are build the
functions and keep data by closure-bindings.
I can build code too, but because of such reasons such functions are
shorter and 'compose can handle them.
Also there is no build-code-phase. Usually i dont compile, i work
directly on the data. So clever macro-support is not that important.
If i need something complex, i write a whole grammar. Maybe more
verbose than backquote-macros, if i want to build code.
But i do not, such a grammar is applied each time to the actual data.
The data is stored efficiently in rebol-values of course, not each
time parsing strings. That "compiling" of data by 'load, and the
block-parser, can often compete with pre-build code.
I dont use 'compose that much to build functions, but to build data.
Rebol-data contains meta-information: keywords, datatypes etc.
msg: compose [message reply "lisp" (now)]
; == [message reply "lisp" 26-Nov-2005/18:48:55+1:00]
Part of many dialects is that datatypes have meaning. Parens in compose.
Or function-headers: refinements have the meaning of optional arguments,
blocks of type-declarations, strings of comments etc.
parse msg [any [
arg: 'message word! (print ["type" arg/2])
| string! (print ["subject" arg/1])
| date! (print ["date" arg/1])
I don't build a function here, i parse data. And compose helps building them.
For a clever generic dialect have a look at Ladislavs 'build. He
basically implements backquoting. The backquotes are 'insert and
'only. And its not that much code. (Although a lot rebol-knowleddge :)
> To unsubscribe from the list, just send an email to
> lists at rebol.com with unsubscribe as the subject.
Any problem in computer science can be solved with another layer of
indirection. But that usually will create another problem.