Question about Function Efficiency

Hi all, I want to create a function that creates a cascade of function applications: cascade [ f1, f2, f3 ] x == (f3 . f2 . f1) x It's a bit trivial, but it makes implementing flowgraphs (which are usually drawn left-to-right) a bit easier. 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)
Thanks.
--
Matthew Donadio

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/
participants (2)
-
David Roundy
-
Matthew Donadio