[Takusen] working out how to use bind variables
dagit at codersbase.com
Thu Aug 12 15:52:33 EDT 2010
On Thu, Aug 12, 2010 at 8:09 AM, Michael Litchard <michael at schmong.org>wrote:
> It works, but I am confused about the final return.
> My understanding is that return takes a computation from one monad,
> and puts it in another . In this case IO (). But the pattern I have
> seen is this,
> return $ some computation
> so when I see
> return ()
> I am not sure what that means.
Let's start with the type of return:
return :: Monad m => a -> m a
It simply takes a value and put it into the monad. For monads that are
containers, like Maybe a, return is extremely simple. For example:
return x = Just x
And () is just a value of type () (pronounced as "unit").
So for the Maybe monad, return () = Just (), with type Maybe ().
In other words, it just puts unit into the monad.
In the context of the IO monad, IO () is the type of a monadic action that
may have side effects but does not produce a meaningful value. Which is
exactly what main does. It has a side effect, but it doesn't produce any
value that your program can use. Similarly for print:
print :: Show a => a -> IO ()
You could do this if you really wanted to:
checkUnit = do
() <- print "Hello, World!"
Or, even give the result a name:
namedUnit = do
x <- print "Hello, World!"
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Takusen