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

More on Tree Folds

17 July, 200729 July, 2007

After some thought, I think I have the definitions for left and right fold on a tree: foldrTree :: (a -> b -> b) -> b -> InternalTree a -> b foldrTree f z ILeaf = z foldrTree f z (IBranch a x y) = foldrTree f (f a (foldrTree…

Read More

Personality profiling

18 November, 2008

I recently took a DISC assessment at work. It’s similar to MBTI which I took a while back at my old company. When I last did MBTI, I came out as ESTP – but almost central on the E/I, S/N, and P/J scales. The only strong preference was T rather…

Read More

Backup solution

30 November, 2007

In these days of digital memories, it’s important to have a proper backup solution for your family. So I’ve invested in a Dlink DNS-323 (a network storage device) and a couple of 500GB hard drives. Plugged it into my network, turned it on, and formatted the drives as RAID 1,…

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