[Haskell-cafe] Question regarding deepseq (Control.DeepSeq)

Ivan Miljenovic ivan.miljenovic at gmail.com
Thu Jun 24 21:11:54 EDT 2010

On 25 June 2010 10:57, Frank Moore <fmoore at gmail.com> wrote:
> Hello Haskellers,
> I am new to programming in Haskell and I am having trouble understanding
> exactly when statements become evaluated.  My goal is to try and measure how
> long a computation takes without having to use a show function.  The code I
> am trying to use is below (taken in part from RWH chapter 25)
> ----------------------------------
> import Data.List (foldl')
> import Data.Time.Clock (diffUTCTime, getCurrentTime)
> import Control.DeepSeq (deepseq)
> mean :: [Double] -> Double
> mean xs = s / fromIntegral n where
>     (n,s) = foldl' k (0,0) xs
>     k (n,s) x = n `seq` s `seq` (n+1,s+x)
> main = do
>   let as = [1..1e7] :: [Double]
>   start <- getCurrentTime
>   let meanOver2 = deepseq (mean as) `seq` (mean as) / fromIntegral 2
>   end <- getCurrentTime
>   putStrLn (show (end `diffUTCTime` start))
>   putStrLn (show meanOver2)
> -------------------------------------
> My understanding of deepseq was that it evaluates (mean as) completely
> before continuing, and then the show would not take any time, but instead
> all the time is spent in the show meanOver2 function.  I feel like I am
> missing something fundamental here.  Any suggestions?  Thanks for your help.

It does... but because you don't save the result it doesn't keep the
result (you're wanting Common Sub-expression Elimination, which GHC
doesn't do).

Try this:

let meanAs = mean as
    meanOver2 = meanAs `deepSeq` meanAs / 2

Note that your usage of "fromIntegral" isn't required, as that is
automatically done for any integral literal (but if you had 2 in an
Int variable then you would need fromIntegral).

Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com

More information about the Haskell-Cafe mailing list