[Haskell-beginners] Re: Forcing evalation in the IO Monad

Daniel Fischer daniel.is.fischer at web.de
Fri Apr 16 11:22:16 EDT 2010


Am Freitag 16 April 2010 17:00:57 schrieb Philip Scott:
>
> The documentation says that, but it does appear to go deeper than just
> one level:
>
> Debug.Trace Prelude Control.Exception> let a = trace "Hello" 42
> Debug.Trace Prelude Control.Exception> let b = trace "Frank" (a * 2)
> Debug.Trace Prelude Control.Exception> evaluate b
> Frank
> Hello
> 84
>
> Perhaps it specifies WHNF in case you 'evaluate' something which doesn't
> have a HNF (like a partially applied function?).


Prelude Control.Exception> let lst = [undefined,undefined] :: [Bool]
Prelude Control.Exception> evaluate lst >>= putStrLn . take 1 . show
[
Prelude Control.Exception> evaluate lst >>= print
[*** Exception: Prelude.undefined

Prelude Control.Exception Debug.Trace> let a = trace "Hello" 42
Prelude Control.Exception Debug.Trace> let b = trace "Frank" [a]
Prelude Control.Exception Debug.Trace> evaluate b
Frank
[Hello
42]
Prelude Control.Exception Debug.Trace> let a = trace "Hello" 42
Prelude Control.Exception Debug.Trace> let b = trace "Frank" [a]
Prelude Control.Exception Debug.Trace> evaluate b >>= print . null
Frank
False

a is only evaluated when it's needed for printing here.

For most number types, weak head normal form is already normal form.
You can only spot the difference on types with more than one constructor or 
a lazy field in a constructor.

>
> - Philip



More information about the Beginners mailing list