[nem-en] Re: [proposal] foldr1/foldl1

Michal Moskal michal.moskal at gmail.com
Tue Jul 10 09:53:45 CEST 2007


On 7/10/07, Elifant <elifantu at mail.ru> wrote:
> IEnumerable.FoldRight is implemented using temporary array (just because
> existing FoldRight
> does that) instead of recursion. Is it preferred way?

Yes, because the IEnumerable can be large, and using recursion could
cause the stack to overflow.

> BTW, I was surprised by the argument order of callback function. I was
> expecting that
>
> [a, b, c, d].FoldLeft(@>>) produces (((a >> b) >> c) >> d), and
> [a, b, c, d].FoldRight(@>>) produces (a >> (b >> (c >> d)))
>
> but in fact I need to use [a, b, c, d].FoldLeft((elem, acc) => acc >> elem)
>
> Haskell, for example, has following (convenient) signatures:
> Prelude.foldl   :: (a -> b -> a) -> a -> [b] -> a  -- accumulator is on
> the left
> Prelude.foldr   :: (a -> b -> b) -> b -> [a] -> b  -- accumulator is on
> the right

There is a reason :-)

In Haskell you use: "foldl f ini lst" -- you pass initial accumulator
before the list, so also the callback takes the accumulator before the
element of the list. In Nemerle, OTOH you write: "lst.FoldLeft(ini,
f)", so the list goes before the accumulator in both function
invocation and the callback.

The callback goes last in Nemerle for two reasons: 1/ it is most
likely the longest expression in invocation, so you don't want any
stuff hanging around after it, and 2/ in Nemerle partial application
can be applied to any argument with the same ease, unlike in ML, so
you can write _.FoldLeft (0, f) and get a function.

> This change will break backward compatibility, but you did it several
> times already,
> so I hope you will want to make this change now, until it is too late.

We've already changed that something like two years ago, for the
reasons described above.

-- 
   Michał


More information about the devel-en mailing list