[plt-scheme] Re: implementation of foldl

From: Jean-Paul ROY (roy at unice.fr)
Date: Wed Apr 29 04:29:26 EDT 2009

In Mathematica (which uses lots of FP paradigms), foldl is Fold :

 > Fold[f,init,{a,b,c,d}]

and behaves like foldl in Haskell. The accumulator is on the left, and  
this may be a good behavior as the computation is done iteratively  
("by hand") from left to right.


> De : "keydana at gmx.de" <keydana at gmx.de>
> Date : 28 avril 2009 22:09:25 HAEC
> À : plt-scheme at list.cs.brown.edu
> Objet : [plt-scheme] implementation of foldl
> Hi all,
> I am wondering whether the order in which foldl applies the "init"  
> argument and the "car" argument to the given combining function is  
> to be regarded as specified or as an implementation detail (such  
> that you should not rely on it).
> Just as an exercise, I was writing reverse with foldl and foldr, and  
> noticed that with "my own" _foldr, defined as
> (define _foldl
>  (lambda (proc initial lst)
>    (if (null? lst)
>        initial
>       (_foldl proc (proc initial (car lst)) (cdr lst)))))
> it worked as
> (define reverse-fl-1
>  (lambda (lst)
>    (_foldl (lambda (x y) (cons y x)) '() lst)))
> whereas for the PLT version of foldl I need
> (define reverse-fl-2
>  (lambda (lst)
>    (foldl (lambda (x y) (cons x y)) '() lst)))

Posted on the users mailing list.