Galt wrote:
make-adder: func [x] [func [y] [+ x y]]
Ladislav pointed out that if you use it more than once,
you must do this:
make-adder: func [x] [func [y] compose[+ (:x) y]]
Galt again:
what is the bloody point of having to put in the compose?
I thought the whole idea here was that contexts were cool,
and you could use them to have a hidden local variable.
I got this from a Scheme example and there's no extra compose
needed there.
I am guessing that the problem is that there is only one
copy of the block [+ x y] and that it has a context, whatever
that is, and that it can only have one context, and that one
gets changed everytime you run make-adder again with a new
parameter value for x. So running make-adder a 2nd time
messes up the value hidden in the context of the first?
with the original simple method, you get this:
>> source add6
add6: func [y][+ x y]
with Ladislav's method, you get this:
>> source add6
add6: func [y][+ 6 y]
which is a nice demonstration.
But it's mechanism doesn't depend on context at all any more.
The function is really returning 6 not X with context.
6 is 6 in any context, so big whoop?
Did this used to work in an older rebol without compose?
I note that this works, too, silly as it is:
>> make-adder: func [x] [func [y] reduce['+ x 'y]]
And this failed to work, which surprised me, so I guess I still don't get it
yet:
>> z: [+ x y]
>> make-adder: func [x] [func [y] bind copy z 'x]
I was hoping that I could make a new copy of the z block each time
make-adder was called, and then make x take on a different value
in the context of each copy.
Maybe somebody could show me how to do it using contexts?
-galt