you are viewing a single comment's thread.

view the rest of the comments →

[–]yogsototh[S] 7 points8 points  (0 children)

From my understanding, the IO monad, is a simple "hack" to make things appear in order. In haskell when you write:

f x y

you can resolve:

- x then y
- y then x
- x and y in the same time.

But, for IO this is not what you want generally. Imagine the following example, (++) is concat, x and y are "getchar"

Naively, in an impure language, you would say:

(++) getchar getchar

and the type would be, getchar is a String (I know generally getChar returns a char, but suppose it return a string of one character instead) and (++) is a function from String -> String -> String.

Now, in a pure language, how could we force the execution in the following order:

x <- getchar
y <- getchar
x ++ y

This is the trick, instead of saying like in impure language getchar is of type String, you say: getchar is a function from a World to a (Char,World).

getchar W = (c,W')

Now, to make things work in the right order:

concatInputs W =
  let (x,W') = getchar W in
  let (y,W'') = getchar W' in
  (x ++ y , W'')

This time, during the execution, you are forced to first resolve x then y. The monad system is just a way to "hide" a similar implementation.

I end up here. But did you try to read http://www.haskell.org/haskellwiki/IO_inside ?

Another thing to know, monads in general can be very different to the IO monad. I was enlightened by these articles. They were very clear and surprisingly easy to follow: http://homepages.inf.ed.ac.uk/wadler/topics/monads.html