[plt-scheme] Behavior of foldl

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Mon Jul 10 11:11:22 EDT 2006

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.

The difference is the argument order to `f'. The variant in "list.ss"
passes the list element first and the accumulator second.

Matthew



Posted on the users mailing list.