Skip to content
Why is a raven like a writing desk?

Thoughts both confusing and enlightening.

Why is a raven like a writing desk?

Thoughts both confusing and enlightening.

Do-notation can be misleading

elbeno, 13 August, 201430 June, 2015

Consider the following function:

oddness :: Maybe Int
oddness = do
  let a = Just 1 :: Maybe Int
  b <- a
  return b

Perfectly fine, albeit contrived, redundant, etc. Bear with me. Now consider what happens if we change the value of a:

oddness :: Maybe Int
oddness = do
  let a = Nothing :: Maybe Int
  b <- a
  return b

This looks odd, because it looks like we're extracting the value from a into b, and then passing it to return - it looks like there's some Int we extract from Nothing, and calling return converts it back to Nothing.

But of course, we know that this do-notation desugars to something like:

oddness :: Maybe Int
oddness =
  let a = Nothing :: Maybe Int
  in a >>= \b -> return b

And recall the definition of (>>=) for Maybe:

instance  Monad Maybe  where
    (Just x) >>= k      = k x
    Nothing  >>= _      = Nothing

So what's happening here is that what's on the right of (>>=) remains unevaluated, and Nothing is the result. Mystery solved.

Haskell Programming Uncategorized

Post navigation

Previous post
Next post

Related Posts

Open barcode database, anyone?

22 September, 200722 September, 2007

Delicious Library is a non-free Mac application that catalogues your library of books, movies, CDs, etc. It does this in an inspired way: apparently you simply hold up the item to your webcam (or iSight as the Mac people call it) and it will read the barcode and discover what…

Read More

Now reading

23 January, 2011

I just installed the Now Reading Reloaded plugin and fiddled with my sidebar template, so you can see what I’m currrently reading. Over time, this will also log what I’ve read, something I’ve been meaning to do for a while.

Read More

April

1 April, 2007

Ah yes, April 1st. The day when the Internet becomes useless because the various editors out there don't seem to understand that it is not an April Fools' joke when every bloody article you read is a bunch of useless tripe. Less is more, people. And since the Internet enables…

Read More

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

©2026 Why is a raven like a writing desk? | WordPress Theme by SuperbThemes