[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] Problem with (fold)
From: |
Peter Bex |
Subject: |
Re: [Chicken-users] Problem with (fold) |
Date: |
Tue, 11 Mar 2014 15:56:49 +0100 |
User-agent: |
Mutt/1.4.2.3i |
On Tue, Mar 11, 2014 at 03:47:53PM +0100, Daniel Carrera wrote:
> On 11 March 2014 15:41, Peter Bex <address@hidden> wrote:
>
> > To avoid such mistakes, it's helpful to use mnemonic names:
> >
> > (fold (lambda (item result) (+ (* result 10) item)) 0 '(1 2 3))
> >
> >
> Thanks. I was mentally reading from left to right, so I ended up assuming
> that it was (result item).
That would make more sense, I guess, and is the ordering used by other
functional languages. There's a built-in version of fold which obeys
this ordering, called foldl:
http://api.call-cc.org/doc/library/foldl
I think SRFI-1's FOLD uses the oher way around is because of the
notion that the procedure argument is called a CONStructor, and "cons"
accepts the item first and the list on which to cons second, making
"reverse" trivial to implement like this:
(fold cons '() '(1 2 3 4)) => (4 3 2 1)
I find it's easy enough to remember if you keep this in mind.
Cheers,
Peter
--
http://www.more-magic.net