[plt-scheme] what is fold-left?

From: Eli Barzilay (eli at barzilay.org)
Date: Wed Feb 11 22:29:50 EST 2009

On Feb 11, Mike Eggleston wrote:
> On Wed, 11 Feb 2009, Eli Barzilay might have said:
> 
> > On Feb 11, Mike Eggleston wrote:
> > > Morning,
> > > 
> > > I'm looking at some code at
> > > <http://funcall.blogspot.com/2007/08/naive-bayesian-classifier.html>. This
> > > code by default will not run in DrScheme. The code is using (fold-left
> > > ...). Looking at the function it looks like a (map ...), but I don't
> > > (yet) know why I can't just use a 'map' instead of the 'fold-left'.
> > > 
> > > Any ideas? What does this function do that 'map' doesn't and why
> > > should it be used?
> > 
> > It's similar to `map' except that you give it a function to accumulate
> > the results.  Think of it as a generic swiss-army-knife of list
> > functions.  In PLT, you have it available as `foldl'.
> 
> Wonderful. Thank you Eli. In scheme how do I alias a function so
> that when 'fold-left' is called it ultimatly calls 'foldl'?
> 
> (define (fold-left &rest args)
>   (apply 'foldl args))

In increasing order of alias-ness:

(define (fold-left kons init list) (foldl kons init list))

or

(define (fold-left . args) (apply foldl args))

or

(define fold-left foldl)

or

(require (rename-in scheme/base [foldl fold-left]))

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.