You can think of a function in the IO monad as returning a shell script that'll eventually be executed by main. Calling the function just creates and returns the script. And no matter how many times you call the same function, it returns the same exact script.
xs = [printStrLn "foo", myPrintStr "bar"]
main = xs !! 1 // !! is zero indexed
This prints "bar" to the screen.
IO actions in Haskell are first-class, but they only get evaluated in a couple circumstances. First of all, main takes an IO action that represents your entire program, and executes it. Secondly, there's unsafePerformIO and unsafeInterleaveIO.
You can just have IO actions floating around your program, and they won't be run (like how printStrLn "foo" isn't run).
4
u/gnuvince Jul 27 '13
How is it saner and easier to understand? If you see this definition in Haskell:
You know it's a pure function, without any side effect.
And this one is also a pure function, however when its return value is performed, it will likely produce some side effect.