[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.