Skip to content

Instantly share code, notes, and snippets.

@JohnL4
Created March 18, 2016 14:07
Show Gist options
  • Save JohnL4/76667683600854c21f47 to your computer and use it in GitHub Desktop.
Save JohnL4/76667683600854c21f47 to your computer and use it in GitHub Desktop.
Example of an unused IO operation in a "do" block executed regardless of the fact that it's unused
import System.IO
main = do
inText <- getContents
let inWords = words inText
result <- f ( read (inWords !! 0) :: Int)
putStrLn result
f :: Int -> IO String
f x = do
h <- openFile "lazyfileplay-out.txt" WriteMode -- This seems to cause the file to be touched regardless of the fact
-- that it's not actually used. Is it because the "do" block just
-- executes everything regardless, as opposed to lazily-binding things
-- and only executing them when needed?
return $ if x == 1 then "1" else "not 1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment