[Haskell-cafe] Re: Tutorial uploaded
Benjamin Franksen
benjamin.franksen at bessy.de
Tue Dec 20 16:04:16 EST 2005
On Tuesday 20 December 2005 20:58, Peter Simons wrote:
> Daniel Carrera writes:
> > I'm scared of monads :) I really don't know what a monad
> > is.
>
> Neither do I, but that doesn't mean that I can't use just
> fine. ;-)
>
> >> putStrLn :: String -> World -> World
> >
> > That seems less scary.
>
> Things become a lot clearer when you think about how to
> print _two_ lines with that kind of function. You'd write:
>
> f :: World -> World
> f world = putStrLn "second line" (putStrLn "first line" world)
>
> The 'world' parameter forces the two functions into the
> order you want, because printing "second line" needs the
> result of printing "first line" before it can be evaluated.
>
> However, writing complex applications with that kind of API
> would drive you nuts,
and would also be unsafe without some kind of strong guarantee that each
single 'world' value is unique. (This is how they do it in Clean.)
Imagine
g :: World -> World
g world = let world' = putStrLn "first line" world
in putStrLn "second line" world -- oops, forgot the "'"
> so instead you can say
>
> f :: IO ()
> f = do putStrLn "first line"
> putStrLn "second line"
>
> and it means the exact same thing.
/and/ it guarantees that 'world' cannot be accidentally duplicated
because it is not directly accessible to the program, thus there is
only one 'world' ever existing at the same time, thus all IO actions
are cleanly sequenced according to the data dependencies.
> Curiously enough, if you check out the reference
> documentation at:
>
>
> http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad-
>ST.html#t%3ARealWorld
>
> ..., you'll find that a "World" type actually exists.
Ben
More information about the Haskell-Cafe
mailing list