[Haskell-cafe] On hGetContents semi-closenesscloseness
es at ertes.de
Wed Feb 16 11:17:54 CET 2011
-----BEGIN PGP SIGNED MESSAGE-----
Brandon S Allbery KF8NH <allbery.b at gmail.com> wrote:
> Haskell is actually what manufacturing folks call "just in time";
> things are evaluated when they are needed. Usually this means that
> when you output something, anything needed to compute that output will
> be done then. The exceptions are things like
> Control.Exception.evaluate (which you can treat as doing output but
> without *actually* outputting anything), mentioned above, plus you can
> indicate that some computation must be evaluated before another by
> means of Prelude.seq. You can also declare a type as being strict by
> prefixing an exclamation mark (so the runtime will always evaluate a
> computation before binding it), and with the BangPatterns extension
> you can also declare a pattern match binding as strict the same way.
Note that pattern matches are strict by default. In fact, a pattern
match is the preferred way to force evaluation. Bang patterns only make
sure that variables (i.e. wildcards, which wouldn't be evaluated
otherwise) in a pattern match are evaluated to WHNF:
case expr of
Just x -> ...
This is strict in the Maybe constructors, but non-strict in the argument
of Just. When using a bang pattern,
Just (!x) -> ...
the match is also strict in the argument of Just. A bang pattern is
really just a shortcut for using 'seq':
Just x -> seq x $ ...
or as some people prefer to write it:
Just x | seq x True -> ...
nightmare = unsafePerformIO (getWrongWife >>= sex)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
-----END PGP SIGNATURE-----
More information about the Haskell-Cafe