[Haskell-cafe] (Read r) => IO (Maybe r)

Jacek Generowicz jacek.generowicz at cern.ch
Fri Dec 17 13:59:10 CET 2010


What are some interesting, idiomatic ways of writing something similar  
to the following, using

a) Only standard utilities

b) Non-standard utilities

getValidatedInteger = do
   maybeInt <- maybeGet
   case maybeInt of
     Just int -> return int
     Nothing  -> do putStrLn "That doesn't seem to be an integer. Try  

maybeGet :: (Read r) => IO (Maybe r)
maybeGet = getLine >>= return . maybeReadS

maybeReadS text =
     case reads text  of
       [(int, rest)] | all (== ' ') rest -> Just int
       _                                 -> Nothing

More information about the Haskell-Cafe mailing list