Why does the function read
terminate the executable when it cannot parse its argument?
It seems to me that it would be more convenient to have a function of type String -> Maybe a
, which returns Nothing
if the string couldn't be parsed to the type a
, but I can't find anything like it. What am I missing?
The notation y=f(x) defines a function named f. This is read as “y is a function of x.” The letter x represents the input value, or independent variable. The letter y, or f(x), represents the output value, or dependent variable.
The function you are looking for is called readMaybe
and is defined in the module Text.Read
:
https://hackage.haskell.org/package/base-4.9.1.0/docs/Text-Read.html#v:readMaybe
It is available since GHC 7.6. Before that you had to call reads
, which returns a (potentially empty) list of parsed values, and pattern match on it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With