
Lennart writes:
euler :: (Num a) => (a -> a -> a) -> a -> a -> [a] euler f' dt f0 = euler' f0 0 where euler' f t = f : euler' fnext (t + dt) where fnext = f + dt * (f' f t)
Jerzy writes:
euler f' dt f0 = map snd (iterate (\(t,f)->(t+dt,f+dt*f' t f)) (0,f0) )
Then Jyrinx writes:
I'm sceptical of Jerzy's code ... Is this likely to result in a faster program, especially what with an optimising compiler like GHC?
Unless I'm missing some important factor, Jerzy's version will run as fast with GHC as Lennart's --faster in some contexts, eg, (map (*2) (euler f' dt f0)), because Jerzy's version is eligible for "fold/build" rewriting. For more info, there's a nice paper on this called "A shortcut to deforestation". Of course, for Haskell newbies, Lennart's "explicit recursion" version is easier to understand, unless you are very comfortable with abstraction like many math types like Jerzy are.