[Haskell-beginners] Evaluation order semantics..

Mike Meyer mike at fpcomplete.com
Mon Sep 23 20:34:38 CEST 2013


王兵兵 wbbtiger at gmail.com  wrote:

Since no one else answered this, I'll take a crack at it.

​> ​
The Haskell language specification states that it is a non-strict
​ ​
​
language,
​> ​
but nothing about the evaluation strategy (like when and how an expression
​> ​
is evaluated, and to what level). It does mention the word "evaluate"
​> ​
several times when talking about pattern matching.
​>​
​> ​
I have read a wonderful tutorial (
​> ​
http://en.wikibooks.org/wiki/Haskell/Laziness
<http://en.wikibooks.org/wiki/Haskell/Laziness>) about lazy evaluation
and
​> ​
weak head normal form, but it is just an implemenation strategy of some
​> ​
compiler, which I should not depend on when writing codes.
​>​
​> ​
I come from a strict language background and I just don't feel right if I
​> ​
don't understand how my codes are execuated. I wonder why the language
​> ​
specificition does not define the evaluation strategy.
​>​
​> ​
I hope someone can enlighten me. Thanks!

​I think you have already achieved enlightenment - you just don't feel
right about it.​

​The language specification doesn't define the evaluation strategy because
it doesn't matter. At least, it doesn't matter in pure code. You'll get the
same answer no matter what order expressions are evaluated in, so long as
they are evaluated by the time they are needed. Not specifying evaluation
order gives the compiler freedom to arrange it to get the best possible
performance.

That said, there are cases where the evaluation order matters. Come to
think of it, most of them fall into two categories: the order matters for
performance reasons (clearly outside the purview of a specification), or
you want to insure that something is evaluated before it's actually used
(like doing IO), and there are tools in the language to force evaluation in
those cases.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130923/8e6fa2c5/attachment.htm>


More information about the Beginners mailing list