[plt-scheme] implementation of foldl

From: Thomas Chust (chust at web.de)
Date: Wed Apr 29 11:25:57 EDT 2009

2009-04-29 keydana at gmx.de <keydana at gmx.de>:
> [...]
> In general, I would be curious to know if the following picture of fold
> usage in scheme would be approximately correct:
>
> - for small lists, if a function can be written with foldl and foldlr,
> choose whichever
> - more functions can only be written with with foldr because they need the
> empty list at the end
> - for very long lists, better use foldl because it uses tail calls
> [...]

Hello Sigrid,

if the list inputs are small and the operator you apply is commutative
it really doesn't matter whether you fold left or right. It also makes
sense to prefer folding left for long linked list inputs since it can
be implemented more efficiently.

Apart from that I think the question is mostly which folding direction
seems more natural for a problem. It depends on the order of input
data and the operator you want to use. I tend to pick the fold
operation that matches the associativity of the operator but one could
also stick with the same fold operation and reverse the inputs if
necessary.


cu,
Thomas


-- 
When C++ is your hammer, every problem looks like your thumb.


Posted on the users mailing list.