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

Re: Currying in Dylan?



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

> What about 'foldr' and 'foldl'?

They are not part of the standard, but an implementation for lists
comes with Gwydion Dylan, in the sequence-utilities module from the
collection-extensions library.

Documentation can be found here:

  http://www.gwydiondylan.org/gdref/gdlibs/libs-collection-extensions-utils.html

The source code (in case you don't want to install the full Gwydion
Dylan) can be found here:

  http://www.ccc.de/cgi-bin/cvsweb/gd/src/common/coll-ext/sequence-utils.dylan?rev=1.3&content-type=text/x-cvsweb-markup

The function "reduce", which is part of the standard, is equivalent to
foldr, but for all sequences. An equivalent of foldl is easily
implemented.

>  Can one pass a function which takes
> two parameters?  i.e.,
> 
>    foldr((+), 0, #(1, 2, 3));
>       => 6

The correct syntax for that would be

  foldr(\+, 0, #(1, 2, 3));

The backslash is used to distinguish the function + from the operator +.

>    do( ((+), 1), #(1, 2, 3));
>       => #(2, 3, 4)

do is only used for side effects, it doesn't collect return
values. map does that for you. So what you want to do would look like
this:

  map(curry(\+, 1), #(1, 2, 3));

or like this (method is Dylan's name for lambda):

  map(method(x) x + 1 end, #(1, 2, 3));

Andreas




References: