[Haskell-beginners] Type unions

aditya siram aditya.siram at gmail.com
Tue Dec 14 21:29:51 CET 2010


Does this help?


data A = A Int

f :: Int -> Either A B
f x
  | even x = Left $ A x     |
  | otherwise = Right $ B x |

-deech

On Tue, Dec 14, 2010 at 2:09 PM, Russ Abbott <russ.abbott at gmail.com> wrote:
> Is there a way to get this to work?
>
> data A = Aconstructor Int
> data B = Bconstructor Int
> data AorB = A | B
> f :: Int -> AorB
> f x
>   | even x     = Aconstructor x
>   | otherwise = Bconstructor x
>
>  I get this diagnostic.
>
> Couldn't match expected type `AorB' against inferred type `A'
>
> Since AorB is A or B, why is this not permitted?
> If instead I write
>
> data AorB = Aconstructor Int | Bconstructor Int
>
> everything works out ok. But what if I want separate types for A and B?
> Thanks,
> -- Russ
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>



More information about the Beginners mailing list