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

New PC

24 September, 200726 September, 2007

I’ve acquired a new PC from the office flea market at the knockdown price of $50. With an Athlon 2200, GeForce 4400, and 1.5GB of RAM, it’s going to be my MAME machine. A significant step up from my previous MAME machine (Athlon 800, GeForce 3, 0.5GB of RAM). My…

Read More

that 12 days of Xmas meme

7 December, 2005

I don't have 12 LJ friends. So you only get to see 4 days' worth. But it's strangely apt even so. My True Love gave to me… 11 s a-waddling. 8 s a-glaring. 6 s a-laughing. 2 hamster s.

Read More

Let’s not go overboard here

16 March, 2011

There is no need to be taking Potassium Iodide, because: Japan is a really long way away, and any release of radioactive material would disperse before reaching the US. Iodine-131 exposure apparently doesn’t increase the risk of thyroid cancer unless you’re a kid. It is likely to do more harm…

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