[Haskell-cafe] Maybe a, The Rationale

Don Stewart dons at galois.com
Sun May 11 22:42:47 EDT 2008


ok:
> > Maybe types force you to deal with it, while simultaneously  
> >providing convenience functions to help you deal with it.
> 
> I readily grant that Maybe is a wonderful wonderful thing and I use it  
> freely and
> voluntarily.  BUT it should not dominate the code.
> 
> Consider Haskell's getChar and hGetChar.  They DON'T return Maybe  
> Char; they
> raise an exception at end of file.  You have to keep testing isEOF/ 
> hIsEOF before
> each character read, as if we had learned nothing since Pascal.
> Arguably, maybeGetChar :: IO (Maybe Char) and hMaybeGetChar :: Handle - 
> > IO (Maybe Char)
> would be a good idea, at least then one could easily set up some  
> combinators to
> deal with this nuisance.

Thankfully its easy to lift IO-throwing things into a safer world.

    import Control.Exception
    import Prelude hiding (getChar)
    import qualified Prelude as P

    getChar :: IO (Maybe Char)
    getChar = handle (const (return Nothing)) (Just `fmap` P.getChar)

-- Don


More information about the Haskell-Cafe mailing list