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

A musical interlude

5 February, 2012

I had piano lessons at around age 5, and again around age 10. I never practised enough or liked my teacher much. As a teenager at university, I went back to the piano to play tunes that I wanted to, and taught myself enough to get by on those. After…

Read More

For what it’s worth

18 November, 200818 November, 2008

Mrs Elbeno and I have been wondering increasingly about how to deal with Mini-Elbeno best regarding preschools etc. This evening we gave him the Burt Reading Test. He is currently 2 years and 7 months old. The results showed a reading age of 8½.

Read More

VS2010 woes: tuples as map keys

20 February, 201530 June, 2015

Another day, another compiler/library bug. If you’re unfortunate enough to still be using Visual Studio 2010, don’t use tuples as map keys. #include #include #include using namespace std; typedef tuple FooT; typedef map MapT; int main(int, char*[]) { MapT m; // put a value in the map { FooT f(nullptr,…

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