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

Saturday…

2 July, 2006

So, England out. Wayne Rooney to be vilified in the press no doubt – and poor penalties from Gerrard and Lampard too. If only they could have played the first 90 minutes like they played in extra time, it might have been a different result. But – Murray puts out…

Read More

Battle of the Bands

8 May, 2009

The buildup started 5-6 weeks ago with band formations and rumours of entries. Then came the posters stuck up across the campus advertising the various bands. Finally, at 5pm today it all kicked off – the second annual Blizzard Battle of the Bands. When I worked at EA, we had…

Read More

Happy $festival

16 April, 2006

Happy first-Sunday-after-the-first-full-moon-on-or-after-the-vernal-equinox. Whatever it is you celebrate this time of year, have a good one.

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