[Haskell] Monads vs. continuations
Gregory Woodhouse
gregory.woodhouse at sbcglobal.net
Mon Oct 31 01:14:29 EST 2005
Newbie alert:
I have some superficial familiarity with continuations as they occur
in traditional denotational semantics, but certainly not a deep
understanding. I also have a special interest in distributed and/or
concurrent processing, so it only seemed natural to me that the monad
concept was one I'd want to confront head on. Now, I warn you: I am
quite new to Haskell (though I have some prior exposure to Scheme and
a background in mathematics). Now, I've just started reading through
Thompson's text and came across this example:
goUntilEmpty :: IO ()
goUntilEmpty
= do line <- getLine
if (line == [])
then return ()
else (do putStrLn line
goUntilEmpty)
Okay, this seems sensible enough. Loosely speaking, I see this code
as getting a line, checking to see if it's empty. If it is, it just
quits (returning the "empty" value). Otherwise, it prints line, and
invokes itself through a *new* do statement. That seems awfully like
using a continuation to track the input stream as part of the
environment. But it seems obvious to me that here is something I'm
not understanding here. I think of the do as providing an appropriate
continuation (one in which the line just read is gone) to pass to the
next call.
Okay, maybe I'm taking things a bit too literally here, but I seem to
recall that a monad is an algebraic object with right and left
identity and an associative composition. I understand the monad here
takes a value (()) and returns an object IO (), and do becomes a
functor of sorts, taking ordinary functions and mapping them to new
functions having their codomain in a new category (an instance of a
monad?) This is where it seems to me that I must be getting the
terminology wrong. Can someone help me out here?
===
Gregory Woodhouse
gregory.woodhouse at sbcglobal.net
"The most incomprehensible thing about
the world is that it is at all comprehensible."
--Albert Einstein (1879-1955)
More information about the Haskell
mailing list