[REBOL] Re: Parsing comment
From: reffy:ulrich at: 25-Sep-2002 7:19
> which is more functional, shall we say, but can we write functions
> that behave like operators? (Or can we write operators, for that
> matter.) ...
>
> >> + 1 2
> == 3
> >> 1 + 2
> == 3
Seems you would need to have a function definition template much like APL has:
Define R Is lop FUNC rop ; locals {block}
Where "R Is" (by its presence) indicates the function returns an explicit result
Where "lop" (by its presence) indicates a left operand
Where "rop" (by its presence) indicates a right operand
Where "FUNC" is the name of the dyadic function (in this case)
Some variations as examples:
NILADIC FUNCTION / no RESULT
Define FUNC {block}
NILADIC FUNCTION / with RESULT
Define R Is FUNC {block}
MONADIC FUNCTION / no RESULT
Define FUNC rop {block}
MONADIC FUNCTION / with RESULT
Define R Is FUNC rop {block}
DYADIC FUNCTION / no RESULT
Define lop FUNC rop {block}
DYADIC FUNCTION / with RESULT
Define R Is lop FUNC rop {block}
NOMADIC FUNCTION / no RESULT
Define [lop] FUNC {block}
NOMADIC FUNCTION / with RESULT
Define R Is [lop] FUNC {block}
Now, for a user defined function to simulate the built-in '+' primitive function:
Define R Is a plus b { R is a + b }
The order of evaluation is assumed to be strictly right to left unless overridden with
parens:
5 * 2 plus 2
(5 * 2) plus 2
=12