Hi Galt,

couldn't you ask simpler questions? {8^D

>
> 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?

Ladislav:
No, the problem is, that Make-adder uses just one  context with
just one 'x during its whole lifetime, that's why you can't  rely
on X's value, if you use Make-adder again.

>
> 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?
>
Here you are:

REBOL
1.0.3.3 (Win32 x86)
TM & Copyright 1998 REBOL Technologies Inc.  All rights reserved.
Send bugs to [EMAIL PROTECTED]
Loading internal rebol.r
REBOL top level.
>> make-adder: func [x] [func [y] [x + y]]
#[function [x] [func [y] [x + y]]]
>> add5: make-adder 5
#[function [y] [x + y]]
>> add6: make-adder 6
#[function [y] [x + y]]
>> add6 1
7
>> add5 1
6

> I note that this works, too, silly as it is:
> >> make-adder: func [x] [func [y] reduce['+ x 'y]]

Ladislav:
Exactly the same result as with Compose.

>
>
> 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
>

I am curious too... (BTW, if you want to look at some higher order
functions, have a look at
http://www.rebol.org/advanced/highfun.r )

Regards
    Ladislav


Reply via email to