Skip to content

Instantly share code, notes, and snippets.

@maoe
Created August 31, 2011 06:23
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 maoe/1182930 to your computer and use it in GitHub Desktop.
Save maoe/1182930 to your computer and use it in GitHub Desktop.
lazy I/Oで出力の順番が意図したとおりにならない例
module Main where
import System.IO.Unsafe (unsafeInterleaveIO)
test f = do
line1 <- f
line2 <- f
putStrLn $ "line2: " ++ line2
putStrLn $ "line1: " ++ line1
main :: IO ()
main = do
putStrLn "lazy"
test $ unsafeInterleaveIO getLine
putStrLn "strict"
test getLine
@maoe
Copy link
Author

maoe commented Aug 31, 2011

% ./UnsafeIO
lazy
aaa
line2: aaa
bbb
line1: bbb
strict
aaa
bbb
line2: bbb
line1: aaa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment