FW: RE [Haskell-cafe] Monad Description For Imperative Programmer
bf3 at telenet.be
Wed Aug 1 10:26:47 EDT 2007
Kaveh> "A monad is like a loop that can run a new function against its variable in each iteration."
I’m an imperative programmer learning Haskell, so I’m a newbie, but I’ll give it a try ☺ Making mistakes is the best way to learn it ;)
There are lots of different kinds of monads, but let’s stick to the IO monad first, which you seem to refer to.
No *an IO monad is not a loop at all*. Instead, from an imperative programmer’s point of view, the following might be better:
“an IO monad is a delayed action that will be executed as soon as that action is needed for further evaluation of the program.”
The simple program
main = getLine >>= putStrLn
can be visually represented as (see attachment)
The “world” (=a representation of your computer’s hardware) is passed to the main function, which passes it to all actions that it encounters during its lazy evaluation, causing the executing of the actions as an effect.
The red wire through which the “world flows” is a “single thread”, it cannot be split (because the physical world cannot be copied!!!), so no unwanted side effects can ever occur, making IO safe in Haskell.
When you write your IO program, this world object is never available (the IO type is a special internal type), so the red wire is erased from the diagram, and the getLine and putStrLn boxes become “delayed actions”.
Imperative programmers like myself might initially be confused when they see Haskell’s do notation, because it looks like the actions are strict statements as in C/C++/Pascal/Java/C#/etc, but they are not.
For example, try the following program:
main = do last [
putStrLn "NOT executed although it is first in the list, as it is not used by the main function!",
putStrLn "This action IS executed because it is evaluated by the main function." ]
This is of course all due to Haskell’s laziness which only evaluates just those expressions that it needs to evaluate the main function.
One thing to note in the diagram above is that the getLine box has TWO outputs, the String and the World. But functions can only have a single output, but this can be tuple. Hence the passing of the world from one box to the other is a bit more complicated. It is this pattern of extracting both values from the output and passing them to the next function and other related combinations that form the generic monad class, which can be used for many more things than IO.
See http://haskell.org/haskellwiki/IO_inside for a much deeper and more correct explanation ☺
And for the pros here, did this newbie make any sense? Probably not ;-)
Oh no, yet another monad explanation!!! Now the universe will most certainly collapse…
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.476 / Virus Database: 269.11.0/929 - Release Date: 31/07/2007 17:26
-------------- next part --------------
A non-text attachment was scrubbed...
Name: IO monad.GIF
Size: 7808 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070801/32e3b083/IOmonad-0001.gif
More information about the Haskell-Cafe