Skip to content

Instantly share code, notes, and snippets.

@gregorycollins
Created October 19, 2011 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregorycollins/1299380 to your computer and use it in GitHub Desktop.
Save gregorycollins/1299380 to your computer and use it in GitHub Desktop.
Evaluation, bang patterns, and deepseq
{-# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment