[Haskell-cafe] Query

Matthew Brecknell haskell at brecknell.org
Sat Feb 10 04:58:37 EST 2007

vishy anand <talbronstien at gmail.com> said:
> hi i am going through yaht tutorial and exercise 4.6 and 4.7..i
> understood
> 4.6,but not 4.7 in which fromTuple (One a ) = Left (Left a ) and
> fromTuple
> (Two a b ) = Left (Right (a,b) ) function r written..why use Either
> type..cant i just say fromTuple (Two a b )=(a,b)

It sounds like you want to write:

-- fromTuple :: Tuple a b c d -> ????
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)

But what would be the type of this function?

While (Two a b) and (Three a b c) are values of the same type, (a,b) and
(a,b,c) are distinct types (because they are defined that way). In a
function with multiple cases, each case must have the same type, so
fromTuple as above fails to type-check. Using Either allows you to write
the four cases with a consistent type.

More information about the Haskell-Cafe mailing list