[racket] question about foldl implementation

From: Joe Marshall (jmarshall at alum.mit.edu)
Date: Fri Aug 13 15:11:16 EDT 2010

It seems to me that the Haskell version is better.
The arguments to foldl are these:  (procedure accumulator list1 list2 ...)
so it makes sense that the call to procedure take the arguments
in the same order.  That is, you end up calling
     (procedure accumulator (car list1) (car list2) ...)

rather than
    (procedure (car list1) (car list2) ... accumulator)

This means that reverse cannot be defined as (foldl cons '() list),
but rather (foldl rcons '() list)  where rcons = (lambda (cdr car)
(cons car cdr))

I think the consistency of keeping the argument list in the same order
trumps the cleverness of a hack definition of reverse (which definition
wouldn't be used anyway).  Also, it is more common to see accumulator
arguments earlier in the argument list than the thing being accumulated.

On Fri, Aug 13, 2010 at 2:42 AM, Noel Welsh <noelwelsh at gmail.com> wrote:
> On Fri, Aug 13, 2010 at 2:53 AM, Will Kurt <wckurt at gmail.com> wrote:
>> So which of these is the canonical implementation of a left fold? Why the
>> difference in Racket?
>> For pure aesthetics I like the behavior of Racket in the case of cons, but
>> for '-' the others seems to make more sense.
> I don't think there is a canonical implementation. In Racket is seen
> as the same function as right fold; it just happens to process the
> list in a different order and so can be tail recursive. Hence it has
> the same interface. Racket values consistency and clarity in code.
> In Haskell left fold is seen as a different function to right fold,
> and hence has a different interface. Haskell values confusing code so
> Haskell programmers appreciate having different interfaces for very
> similar functions (and the type system will catch some errors).
> HTH,
> N.
> PS: Some of the above is not to be taken seriously.
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users


Posted on the users mailing list.