[Haskell-cafe] Errors in non-monadic code

Daniel F 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:

> Hi,
>
>
Hello!


> 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 [1] 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
>
>
> Cheers,
> Jose
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

[1] http://hackage.haskell.org/package/errors

-- 
Sincerely yours,
-- Daniil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130819/5b699264/attachment.htm>


More information about the Haskell-Cafe mailing list