# [plt-scheme] Behavior of foldl

It is non-standard, but not in the way you are imagining. The order of
the arguments to `f' (the first parameter to foldl) are reversed from
what you expect (and from what Haskell does). I did that because
`foldl' accepts an arbitrary number of list arguments (and thus `f' a
matching number of arguments) and I thought it would be easier to use
in that case if the accumulator came at the end, but I'm having trouble
remembering exactly why now .. it was quite a while ago.
Robby
At Sun, 9 Jul 2006 19:48:55 +0200, Jean-Paul ROY wrote:
>* It seems that the behavior of foldl is non standard with respect to
*>* most functional programming languages.
*>* Il should be the iterative version of foldr, working from left to
*>* right :
*>*
*>* (define (foldl f e L)
*>* (define (iter acc L)
*>* (if (null? L)
*>* acc
*>* (iter (f acc (car L)) (cdr L))))
*>* (iter e L))
*>*
*>* > (foldl (lambda (x y) (+ (* 2 x) y)) 4 '(1 2 3))
*>* 43
*>*
*>* But the fold defined in list.ss has not this behavior and returns 16.
*>*
*>* See example 5 in http://www.zvon.org/other/haskell/Outputprelude/
*>* foldl_f.html
*>*
*>* Am i wrong ?
*>*
*>* Jean-Paul Roy
*>*
*>* _________________________________________________
*>* For list-related administrative tasks:
*>* http://list.cs.brown.edu/mailman/listinfo/plt-scheme
*