[Haskell-cafe] Question concerning datatype "Either"

phy51km4n phy51km4n at yahoo.de
Fri Jul 4 04:57:49 EDT 2008

There is a exercise using datatype "Either" I'm a bit confused about...

The following datatypes are given:

data Either a b = Left  a 
                | Right b

data Tuple a b c d = One   a
                   | Two   a b
                   | Three a b c
                   | Four  a b c d

Now the exercise:

"Based on our definition of Tuple, write a function which takes a Tuple and
returns either the value 
(if it’s a one-tuple), a Haskell-pair (i.e., (’a’,5)) if it’s a two-tuple, a
if it’s a three-tuple or a Haskell-quadruple if it’s a four-tuple. You will
need to 
use the Either type to represent this."

Why does that not work? :

fromTuple (One   a)       =  a
fromTuple (Two   a b)     = (a, b)
fromTuple (Three a b c)   = (a, b, c)
fromTuple (Four  a b c d) = (a, b, c, d)

Why is this correct? :

fromTuple (One   a )      = Left  (Left   a)
fromTuple (Two   a b )    = Left  (Right (a,b))
fromTuple (Three a b c )  = Right (Left  (a,b,c))
fromTuple (Four  a b c d) = Right (Right (a,b,c,d))

Why does this combination of Rights and Lefts work and how does it work??

View this message in context: http://www.nabble.com/Question-concerning-datatype-%22Either%22-tp18264739p18264739.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

More information about the Haskell-Cafe mailing list