[racket] question about foldl implementation

From: Will M. Farr (wmfarr at gmail.com)
Date: Fri Aug 13 18:21:24 EDT 2010

+1 from me, too.  I always have to check myself to remember that the foldl accumulator argument comes in on the *right*.  FWIW, OCaml also does it the Haskell way (foldl accumulator on the left, foldr accumulator on the right).  

This also makes the obvious many-argument version more efficient:

(define (foldl kons knil . lists)
  (if (ormap null? lists)
    knil
    (apply foldl kons (apply kons knil (map car lists)) (map cdr lists))))

as opposed to

(define (foldl* kons knil . lists)
  (if (ormap null? lists)
    knil
    (apply foldl* kons (apply kons (append (map car lists) (list knil))) (map cdr lists))))

The extra append is kinda annoying in the foldl* version....

Will

On Aug 13, 2010, at 4:18 PM, Matthias Felleisen wrote:

> 
> I privately +1ed Joe, and I all supportive of introducing new folds and phasing out the old ones. 
> 
> 
> On Aug 13, 2010, at 4:04 PM, Sam Tobin-Hochstadt wrote:
> 
>> On Fri, Aug 13, 2010 at 3:11 PM, Joe Marshall <jmarshall at alum.mit.edu> wrote:
>>> It seems to me that the Haskell version is better.
>> 
>> The Haskell ordering also has the advantage of fitting with the Typed
>> Racket type system for variable-arity functions.
>> -- 
>> sam th
>> samth at ccs.neu.edu
>> _________________________________________________
>> For list-related administrative tasks:
>> http://lists.racket-lang.org/listinfo/users
> 
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users



Posted on the users mailing list.