问题
According to this article on denotational semantics in haskell All types have bottom, and a function f:A->B is strict if it maps the bottom of type A to the bottom of type B, it is called non-strict other-wise.
(This is reminiscent of a pointed category where morphisms preserve the basepoint).
Why does Haskell have non-strict functions, whereas Standard ML doesn't?
回答1:
Every programming language with recursion has at least one non-strict function, often in the form of a conditional (if-then-else
). Otherwise all recursions would denote bottom (non-termination). Essential as non-strict functions are, however, most of these languages do not let you define your own! Some languages make up for this limitation by offering macros--a somewhat function-like mechanism that transforms syntax instead of values.
回答2:
Why does Haskell have non-strict functions, whereas Standard ML doesn't?
Haskell has non-strict functions -- typically lazy ones -- because they are a useful programming feature to have.
They improve equational reasoning, make it easier to compose code, and make it possible to write more kinds of programs.
回答3:
Simon Peyton-Jones gave some good responses to this in his set of slides, Wearing the Hair Shirt.
Laziness is jolly convenient
Recursive values are jolly useful
Laziness keeps you honest [regarding purity]
The last reason is the most important to me. Haskell's computational purity and tight control of effects are due in large part to its non-strictness.
Every call-by-value language has given into the siren call of side effects
Programmers want to write C-like code, which I think is the "siren call" that lures in most languages. In Haskell, interleaving effects willy-nilly just doesn't make sense, because non-strictness means that you wouldn't be sure when an effect will be performed.
回答4:
Why does Haskell have non-strict functions, whereas Standard ML doesn't?
Because, expression in haskell are evaluated in Weak Head Normal Form, whereas in Standar ML expression are evaluated in Normal Form.
Then, in Haskell you can reason using unevaluated thunk in Standard ML you can't.
But, you should know that laziness can be added to Standard ML. (you can do that in ocaml for example)
Laziness by default in haskell is a design choice, may be, reflecting the belief that creating a compiler which deal with lazy by default could improve understanding of Functional Programming, allowing the research community to go one step forward.
来源:https://stackoverflow.com/questions/14261728/why-does-haskell-have-non-strict-functions-semantics