[Haskell-beginners] 'read' throws; why not maybe or either?

Michael Orlitzky michael at orlitzky.com
Mon Nov 25 16:37:43 UTC 2013


On 11/24/2013 04:49 PM, Patrick Redmond wrote:
> Is there a variation of 'read' which uses Maybe or Either to handle
> failure instead of an exception?

readMaybe:

http://hackage.haskell.org/package/base-4.6.0.1/docs/Text-Read.html#v:readMaybe

  Prelude> import Text.Read (readMaybe)
  Prelude Text.Read> readMaybe "Hello, World!" :: Maybe Int
  Nothing
  Prelude Text.Read> readMaybe "45" :: Maybe Int
  Just 45



More information about the Beginners mailing list