[Haskell-cafe] Errors in non-monadic code
difrumin at gmail.com
Mon Aug 19 20:09:13 CEST 2013
On Mon, Aug 19, 2013 at 9:48 PM, <jabolopes at google.com> wrote:
> What is the proper way to implement a non-monadic function that checks
> whether a given value is correct and gives a proper error message
> otherwise ? What is the recommended option ?
I am not sure, what do you mean by non-monadic. Both (Either String) and
Maybe are monads.
You can pick up whatever option you like, depending on which option, in
your opinion, suits you better for your specific case.
There is also a helpful errors  package that provide convenient means of
converting between the results of the two approaches.
Control.Error.Util.hush :: Either a b -> Maybe b
Control.Error.Util.note :: a -> Maybe b -> Either a b
> * Either String a
> check val
> | valid val = Right val
> | otherwise = Left errorMsg
> * Maybe String
> check val
> | valid val = Nothing
> | otherwise = Just errorMsg
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe