A newbie's proud first code
Tue, 06 Nov 2001 12:09:24 +0100
Jyrinx likes the expanded Lennart's version of the diff.eq. solver:
> > > 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)
> > >
> > > -- Lennart
> But I'm skeptical about Jerzy's code:
> > euler f' dt f0 = map snd (iterate (\(t,f)->(t+dt,f+dt*f' t f)) (0,f0) )
> Is this likely to result in a faster program, especially what with an
> optimising compiler like GHC? I mean, a one-line version is nice, but the
> longer ones seem more intuitive to me. (I also play with Python, whose
> philosophy is that expressiveness is more important than the absolute
> minimum of code.)
First, you ask about whether it is fast, and then you admit that the
expresiveness may be more important than the code obfuscatominimalism.
Of course that longer versions are more readable.
And they may be more efficient, because the code is direct, without
I enjoy being crazy, but everything has its limits, and beginners should
be protected from such code. It was for fun. But serious.
Still, the author asked for that. He presented his code, confessed his
admiration for its compactness, and asked for comments about the style.
Well, the usage of functionals such as folds, iterates, maps, zips, etc.
IS an archetypical feature of functional languages, and using them as
a replacement of typical recursive patterns may be encouraged. Koen adds
to it the comprehensions, rrright.
But comprehensions (and maps) in an incremental context (requiring rather
folds than maps), have the property that they need *recursive data*
(auto-referential) to be constructed, and this may not be easy to digest
by newbies, even if they know what the recursion is. This is, on the
other hand, a specific archetype for lazy languages.