I am reading the real world haskell Chapter 9.
There is a maybeIO function wraps a IO function result into a Maybe:
maybeIO :: IO a -> IO (Maybe a)
maybeIO act = handle(\_ -> return Nothing) ( liftM Just act)
but this code wont work in GHC 7 or +, i wonder how to edit this code so it will work in GHC 7, i tried
import Control.Exception (bracket, handle, SomeException(..))
maybeIO :: IO a -> IO (Maybe a)
maybeIO act = handle(\(SomeException e) -> return Nothing) ( liftM Just act)
but i got a Parse error: naked expression at top level
I didn’t get any such parse error – the only error I got was from missing functions:
You need to
import Control.Monadwhich is where theliftMfunction is defined. That or usefmapinstead ofliftM.Your parse error might be due to incorrect indentation, but a copy and paste of your code results in no such issues for me.