public
Created

Evaluation, bang patterns, and deepseq

  • Download Gist
ensure.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
{-# LANGUAGE CPP, BangPatterns #-}
 
module Main where
 
import Control.DeepSeq
import Data.List
 
ensure :: [Int] -> IO [Int]
ensure xs = xs `deepseq` return xs
 
testList :: [Int]
testList = [1..100] ++ [error "if we get here, the whole list is evaluated"]
 
main :: IO ()
main = do
!r <- ensure testList
putStrLn "we survived without evaluating r"
let x = foldl' (+) 0 $ take 50 r
putStrLn $ "x was " ++ show x

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.