Question about Function Efficiency

David Roundy droundy@abridgegame.org
Thu, 22 May 2003 15:46:36 -0400


On Thu, May 22, 2003 at 03:24:08PM -0400, Matthew Donadio wrote:
> 
> I plan on profiling this, but which of the following do you think is the
> most efficient implementation:
> 
> > cascade1 :: Num a => [[a] -> [a]] -> [a] -> [a]
> > cascade1 []     = \x -> x
> > cascade1 (f:fs) = cascade1 fs . f
> 
> > cascade2 :: Num a => [[a] -> [a]] -> [a] -> [a]
> > cascade2 cs = foldr (.) (\x -> x) (reverse cs)
> 
> > cascade3 :: Num a => [[a] -> [a]] -> [a] -> [a]
> > cascade3 cs = foldl (.) (\x -> x) (reverse cs)

I'd guess the first, since it eliminates the reverse.  If you want to use a
fold, why not avoid the reverse with something like

cascade4 cs x = foldl (\x f->f x) x cs

which I find a bit easier to read.
-- 
David Roundy
http://civet.berkeley.edu/droundy/