[Prev][Next][Index][Thread]

Re: Currying in Dylan?



Zeno <zenem@nospam.earthlink.net> writes:

> What about 'foldr' and 'foldl'?  Can one pass a function which takes
> two parameters?  i.e.,
> 
>    foldr((+), 0, #(1, 2, 3));
>       => 6
> 
> ...or...
> 
>    do( ((+), 1), #(1, 2, 3));
>       => #(2, 3, 4)

Yes, with map for example you can do:

  map(\+, #(1,2,3), #(4,5,6));
    => $0 = #(5, 7, 9)

Note that 'do' doesn't return anything. It's only used for side
effects. You can also curry. Life the following:

  map(curry(\+, 1), #(1, 2, 3));
    => $2 = #(2, 3, 4)

Or do anonymous methods:

  map(method(a) a + 5 end, #(1, 2, 3));
    => $3 = #(6, 7, 8)

For foldr, there is reduce:

  reduce(\+, 0, #(1, 2, 3));
    => $5 = 6

Chris.
-- 
http://www.double.co.nz/dylan




Follow-Ups: References: