[Haskell-beginners] Lazy vs Strict ponderings...
Heinrich Apfelmus
apfelmus at quantentunnel.de
Sat Mar 19 12:50:18 CET 2011
Sean Charles wrote:
> I *think* I understand about lazy evaluation and its effects on I/O and
> that it can internally create thunks, promises, continuations etc.
> whatever you want to call them, and then at some point there will be a
> sudden spike in memory and CPU loads as something triggers a whole chain
> of promises to be fulfilled but what I don't have a feel for is just how
> serious a problem that might be today in say, a simple desktop application.
>
> [...]
>
> Is it something one needs to worry about at all or should one just code
> away and write the application and then worry!?
>
> I think that having a clearer understanding of what 'types' of problems
> and their implementations have on CPU/RAM would be a good one to have!
The practitioner's approach to laziness is usually the following
sequence of approximations, which could be called "the lazy approach to
laziness":
0) Don't worry about performance. Write programs that are correct
instead. Laziness helps a lot with writing clean and compositional code.
Example:
zipWith (,) [1..] xs
1) Look out for common laziness gotchas. There are essentially only two
of them:
i) Unevaluated expressions that take way more space
than their evaluated forms. Prototypical example
foldl (+) [1..n]
ii) Memory leaks due to sharing. Prototypical example
average xs = sum xs / length xs
2) Don't worry about memory leaks until they actually appear. If they
do, use a profiling tool to find out what's going on. Most likely, one
of the two things above happened in an interesting way.
Note that you might want to be a bit more careful when writing a real
time system. In that case, would suggest to only use abstractions whose
performance is easy to reason about and to keep the core code fairly
small, so that you have a clear picture of what is going on. Other than
that, there is no need to worry about garbage collection or laziness.
Regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
More information about the Beginners
mailing list