[Haskell-beginners] = vs <-

Kyle Murphy orclev at gmail.com
Thu Aug 12 10:39:42 EDT 2010

To really understand the difference between = and <- you need to
understand the syntactic sugar that the do notation provides. Given
the following:

myExample :: Int -> IO Int
myExample num = return num

You could write the following:

main = do
  theNum <- myExample 42
  putStrLn (show theNum)

This is exactly the same as writing the following:

main = (myExample 42) >>= (\theNum -> putStrLn (show theNum))

The type of >>= is:

m a -> (a -> m b) -> m b

Remember that in most cases, once something is "inside" of a monad (IO
in this case), it's very difficult, impossible, to get it out again.
What >>= provides, is a way of taking a value out of a monad, using
some function on it that returns a new value inside that same monad,
and then returning that. For example, once the types get bound, the
>>= in the above example has type:

IO Int -> (Int -> IO ()) -> IO ()

The m has been bound to IO, a is bound to Int, and b has been bound to
(). This is because the type of putStrLn is String -> IO (). The do
syntax just adds a convenient abstraction so that you don't have to
construct complex chains of >>= and >> operations by hand. In our
limited example this is fairly trivial, but consider something like:

main = do
  something <- foo
  blah <- bar something
  moreblah <- baz something blah
  putStrLn moreblah
  putStrLn "really long chain now"

which would be translated to:

main = foo >>= (\something -> bar something >>= (\blah -> baz
something blah >>= (\moreblah -> putStrLn moreblah >> (putStrLn
"really long chain now"))))

Conceptually you can think of (<-) as having type:

a <- IO a

although since (<-) isn't really an op (it's syntactic sugar that's
only valid inside the do notation, it doesn't exist after you remove
the sugar) it of course doesn't really have a type. This also why you
can't just use (=) inside of do notation, and instead have to use let,
you need to escape out of the sugar before you can use it. For an
example of why this is, consider the following:

main = do
  something = foo
  putStrLn something

which would be translated into:

main = something = foo >>= (putStrLn  something)

which of course doesn't make any sense at all.

-R. Kyle Murphy
Curiosity was framed, Ignorance killed the cat.

On Wed, Aug 11, 2010 at 16:12, prad <prad at towardsfreedom.com> wrote:
> i'm using them right i think because it works, but my understanding is
> fuzzy.
> it seems <- is used when you do things like load a file or get
> arguments from outside or if you are return something from a function
> these seem to take the form of something other than an internal type
> like Int or String and have to be 'massaged' into use with things like
> show or fromSql etc
> the = is used with let and seems to work for any assignments that are
> internal.
> are these assessments correct?
> --
> In friendship,
> prad
>                                      ... with you on your journey
> Towards Freedom
> http://www.towardsfreedom.com (website)
> Information, Inspiration, Imagination - truly a site for soaring I's
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners

More information about the Beginners mailing list