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

Daniel Fischer daniel.is.fischer at web.de
Fri Apr 16 15:41:56 EDT 2010


Am Freitag 16 April 2010 20:50:21 schrieb Philip Scott:
> > 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
>
> Ahh, yes you are of course right. Hmm so, how do we crack this nut
> without resorting to deepSeq etc.. It must be possible, because it
> happens when you print it!

When you print it, show demands that the value be completely evaluated. If 
the data-dependencies require an expression to be completely evaluated, it 
will be done. And that's basically the only way to get expressions 
evaluated. 'seq' and its relatives only give you a means to introduce new 
(artificial) data-dependencies.

>
> Am I going to have to resort to physically writing the output to
> /dev/null ?

I think deepseq/rnf from Control.DeepSeq and NFData instances may be easier 
than writing stuff to /dev/null.

>
> It seems like such an ordinary thing to want to do,

Not really. Wanting something to be completely evaluated without a direct 
need from data-dependencies is something not too ordinary in a lazy 
language. Of course, you sometimes need it, that's why there are seq and 
NFData. But it remains something special.

> the fact that it is
> hard makes me think I must be approaching this in the wrong way (which
> is usually the case when I get my knickers in a twist with Haskell ;)
>
> - Phil



More information about the Beginners mailing list