
Hello I'm new to Haskell, and need help with figuring out the Either type... Any example to show how this works? Jacob

Often, Either is used to represent, exclusively, a value or a failure, in a
more detailed way than Maybe can. For example, a function like `parse` (
http://hackage.haskell.org/packages/archive/parsec/latest/doc/html/Text-Pars...),
which is part of Parsec, might have a type like:
parse :: [...] s ->
Eitherhttp://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Data-Eithe...
ParseErrorhttp://hackage.haskell.org/packages/archive/parsec/latest/doc/html/Text-Pars...
a
Meaning, parsing will either fail with a Left ParseError, or succeed with a
Right a, where a is whatever type your parser returns.
Hope that helps,
Alvaro
On Wed, Feb 6, 2013 at 9:37 PM, Jacob Thomas
Hello
I'm new to Haskell, and need help with figuring out the Either type... Any example to show how this works?
Jacob
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Am 07.02.13 03:37, schrieb Jacob Thomas:
Hello
I'm new to Haskell, and need help with figuring out the Either type... Any example to show how this works?
Maybe a program I wrote a while ago can help you get it.
https://github.com/ctbo/slitherlink
In Slitherlink.hs there is a function readProblem that can fail. It
returns either a String with an error message (Left) or the problem read
(Right).
[...]
readProblem :: String -> Either String Problem
readProblem s = do
pl <- readProblemList s
when (null pl) $ Left "Problem is empty."
let columns = length $ head pl
when (columns == 0) $ Left "Problem starts with an empty line."
unless (all ((== columns) . length) pl) $ Left "Problem not
rectangular."
let rows = length pl
return $ listArray ((0, 0), (rows-1, columns-1)) $ concat pl
[...]
Or if you don't feel comfortable with using the Either monad, look at
the even simpler function readConstraint a few lines earlier in the same
file.
In the main solve.hs program this is used to output a meaningful error
message to the user if the input file is invalid:
[...]
where work s n = case readProblem s of
Left e -> putStrLn e
Right p -> do
[...do something with the problem p...]
Hope this helps.
Harald
--
Harald Bögeholz

Hi Jocob
I would recommend you to go through the LYH (
http://learnyouahaskell.com/making-our-own-types-and-typeclasses ) .
data Either a b = Left a | Right b deriving (Eq, Ord, Read, Show)
Lets say you have a division function and you want to avoid division by
zero so this simple function
simpleDiv :: Int -> Int -> Int
simpleDiv m n = div m n
will through error and stop executing rest of you code ( See more on error
handling ) so you can write your function which can handle this
division :: Int -> Int -> Either String Int
division m n
| n == 0 = Left "Division by zero"
| otherwise = Right $ div m n
You can extend this solution as you wish and lets say you want both , some
times integer division and some times floating point division based on
flag. You set you flag true for floating division and false for integer
division.
data Either' a b c = Left a | Mid b | Right c deriving ( Show , Eq )
tempFunction :: Int -> Int -> Bool -> EIther' String Double Int
tempFunction m n f
| n == 0 = Left "Division by zero"
| f = Mid $ m / n
| otherwise = Right $ div m n
I haven't tested this code but the idea is if you want to return different
results then you use this. Also see Use of Either (
http://book.realworldhaskell.org/read/error-handling.html ). Hopefully I
have explained it correctly.
--Mukesh
On Thu, Feb 7, 2013 at 8:07 AM, Jacob Thomas
Hello
I'm new to Haskell, and need help with figuring out the Either type... Any example to show how this works?
Jacob
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (4)
-
Alvaro Gutierrez
-
Harald Bögeholz
-
Jacob Thomas
-
mukesh tiwari