[Haskell-cafe] Question concerning datatype "Either"

Bayley, Alistair Alistair.Bayley at invesco.com
Fri Jul 4 05:22:14 EDT 2008

> From: haskell-cafe-bounces at haskell.org 
> [mailto:haskell-cafe-bounces at haskell.org] On Behalf Of phy51km4n
> 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)

Have you tried it? What are the types of the various cases in fromTuple
(you can rename them to get ghci to accept them as separate functions):

fromTuple1 :: Tuple a b c d -> a
fromTuple1 (One a) = a

fromTuple2 :: Tuple a b c d -> ???
fromTuple2 (Two a b) = (a, b)

> 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))

Again, what is the type of just this line?

fromTuple (One   a )      = Left  (Left   a)

and this line?

fromTuple (Two   a b )    = Left  (Right (a,b))

And if you combine them?

fromTuple :: Tuple a b c d -> ???
fromTuple (One   a )      = Left  (Left   a)
fromTuple (Two   a b )    = Left  (Right (a,b))

(You should see that Either's type parameters are progressively
filled-in as you add more cases.)

Confidentiality Note: The information contained in this message,
and any attachments, may contain confidential and/or privileged
material. It is intended solely for the person(s) or entity to
which it is addressed. Any review, retransmission, dissemination,
or taking of any action in reliance upon this information by
persons or entities other than the intended recipient(s) is
prohibited. If you received this in error, please contact the
sender and delete the material from any computer.

More information about the Haskell-Cafe mailing list