[Haskell-beginners] Handling failed output

Mason Lai masonmlai at gmail.com
Mon Apr 25 19:47:25 UTC 2016


I know you're asking for a function that will either return an Int or a
(Int, Int). However, in my limited experience, I haven't seen Ints used as
error codes in Haskell. Typically, I see something like the following:

checkNum3 :: Int -> Int -> Either String (Int,Int)
checkNum3 a b = if check a b
    then Right (a, b)
    else Left "Error: something happened"

Then you can use a function like (either error id) to either extract the
(Int, Int) value or to raise the error with the message.

The (either error id) function will look at an Either value. If it's a Left
value, it will call error on that value -- so (error "Error: something
happened"). If it's a Right value, it will call id on that value -- so id
(a, b) which evaluates to (a, b).

I usually use this setup if I lazily parse a huge file of text data, where
each line has some constraint that makes the file "valid" or "invalid". I
end up with a long list of Either String (Int, Int)s, and I then map
(either error id) over the list.

On Sat, Apr 23, 2016 at 2:05 PM, Imants Cekusins <imantc at gmail.com> wrote:

> Hello Matt,
>
> Either Int (Int,Int) might work.
>
> Left ... by convention indicates 'other' result.
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160425/9d359c90/attachment.html>


More information about the Beginners mailing list