[Haskell-beginners] Help in pattern matching

Stephen Tetley stephen.tetley at gmail.com
Mon Mar 8 06:28:16 EST 2010


Hi Magnus

That's not quite equivalent to Joe Fox's original code, although it is
an improvement as test in the original won't type check:

original...

test (DataConst1 x y) = x   -- returns Int
test (DataConst2 x y) = x   -- returns Int
test (DataConst3 x y) = y   -- returns String
test (DataConst4 x y) = y   -- returns Int


This one does type check...

test2 :: TestData -> Either Int String
test2 (DataConst1 x y) = Left x
test2 (DataConst2 x y) = Left x
test2 (DataConst3 x y) = Right y
test2 (DataConst4 x y) = Left y



On 8 March 2010 10:26, Magnus Therning <magnus at therning.org> wrote:

> AFAICS the following would be equivalent to your code:
>
> type TestData2 = (Either String Int, Either String Int)
> test2 (x@(Right _), _) = x
> test2 (Left _, y) = y
>


More information about the Beginners mailing list