Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3932522
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:28:06+00:00 2026-05-19T23:28:06+00:00

[This question is motivated by Chapter 9 in Real World Haskell] Here’s a simple

  • 0

[This question is motivated by Chapter 9 in “Real World Haskell”]

Here’s a simple function (cut down to essentials):

saferOpenFile path = handle (\_ -> return Nothing) $ do
      h <- openFile path ReadMode
      return (Just h)

Why do I need that $ ?

If the second argument to handle isn’t a do block, I don’t need it. The following works just fine:

handle (\_ -> putStrLn "Error calculating result") (print x)

When I tried removing the $ compilation failed. I can get it to work if I explicitly add parens, i.e.

saferOpenFile path = handle (\_ -> return Nothing)  (do
      h <- openFile path ReadMode
      return (Just h))

I can understand that, but I guess I’m expecting that when Haskell hits the do it should think “I’m starting a block”, and we shouldn’t have to explicitly put the $ there to break things up.

I also thought about pushing the do block to the next line, like this:

saferOpenFile path = handle (\_ -> return Nothing)  
  do
    h <- openFile path ReadMode
    return (Just h)

but that doesn’t work without parens either. That confuses me, because the following works:

add a b = a + b

addSeven b = add 7
   b

I’m sure I’ll just reach the point where I accept it as “that’s just how you write idiomatic Haskell”, but does anyone have any perspective to give? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:28:07+00:00Added an answer on May 19, 2026 at 11:28 pm

    According to the Haskell 2010 report, do desugars like this:

    do {e;stmts} = e >>= do {stmts}

    do {p <- e; stmts} = let ok p = do {stmts}
                             ok _ = fail "..."
                         in e >>= ok
    

    For the second case, it’s the same to write it desugaring like this (and easier for demonstrative purposes):

    do {p <- e; stmts} = e >>= (\p -> do stmts)

    So suppose you write this:

    -- to make these examples shorter
    saferOpenFile = f
    o = openFile
    
    f p = handle (\_ -> return Nothing) do
        h <- o p ReadMode
        return (Just h)
    

    It desugars to this:

    f p = handle (\_ -> return Nothing) (o p ReadMode) >>= (\h -> return (Just h))
    

    Which is the same as this:

    f p = (handle (\_ -> return Nothing) (o p ReadMode)) >>= (\h -> return (Just h))
    

    Even though you probably intended for it to mean this:

    handle (\_ -> return Nothing) ((o p ReadMode) >>= (\h -> return (Just h)))
    

    tl;dr – when do syntax is desugared, it does not parenthesize the entire statement like you would think it should (as of Haskell 2010).

    This answer is only AFAIK and

    1. may not be correct
    2. may be outdated someday if it is correct

    Note: if you say to me “but the actual desugaring uses a ‘let’ statement that should group e >>= ok, right?”, then I would answer the same as the tl;dr for do statements: it doesn’t parenthesize/group like you think it does.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.