
Hi Guys , I am having a problem with patter matching . I have declared following data type data TestData = DataConst1 Int String | DataConst2 Int Int | DataConst3 Sting String | DataConst4 String Int and now i have function *"test"* which behaves similarly for (DataConst1 and DataConst2 ) and behaves similarly from DataConst3 and DataConst4. for ex test (DataConst1 x y) = x test (DataConst2 x y )= x test (DataConst3 x y) = y test(DataConst4 x y )= y no my problem here is , is there any way i can merge these 4 statements in two statements .... some way i can match DataConst1 and DataConst2 in the same line it would save me a lot of rewriting of code

Maybe you want to use this type instead?
type TestData = (Either String Int, Either String Int)
There is also the function either which can save you some pattern matching.
There are some errors in your code. What is the type of test?
On 8 March 2010 10:11, Joe Fox
Hi Guys ,
I am having a problem with patter matching . I have declared following data type
data TestData = DataConst1 Int String | DataConst2 Int Int | DataConst3 Sting String | DataConst4 String Int
and now i have function "test" which behaves similarly for (DataConst1 and DataConst2 ) and behaves similarly from DataConst3 and DataConst4.
for ex
test (DataConst1 x y) = x test (DataConst2 x y )= x
test (DataConst3 x y) = y test(DataConst4 x y )= y
no my problem here is , is there any way i can merge these 4 statements in two statements .... some way i can match DataConst1 and DataConst2 in the same line it would save me a lot of rewriting of code
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Mon, Mar 8, 2010 at 09:11, Joe Fox
Hi Guys ,
I am having a problem with patter matching . I have declared following data type
data TestData = DataConst1 Int String | DataConst2 Int Int | DataConst3 Sting String | DataConst4 String Int
and now i have function "test" which behaves similarly for (DataConst1 and DataConst2 ) and behaves similarly from DataConst3 and DataConst4.
for ex
test (DataConst1 x y) = x test (DataConst2 x y )= x
test (DataConst3 x y) = y test(DataConst4 x y )= y
no my problem here is , is there any way i can merge these 4 statements in two statements .... some way i can match DataConst1 and DataConst2 in the same line it would save me a lot of rewriting of code
I don't believe there is. AFAIU pattern matching is made on constructors, and I've never come across any way of abstracting over several constructors in a single pattern. As was suggested by Jonas, it might be worth exploring a tuple of two Either instead. /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus@therning.org Jabber: magnus@therning.org http://therning.org/magnus identi.ca|twitter: magthe

On Mon, Mar 8, 2010 at 10:21, Magnus Therning
As was suggested by Jonas, it might be worth exploring a tuple of two Either instead.
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 /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus@therning.org Jabber: magnus@therning.org http://therning.org/magnus identi.ca|twitter: magthe

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

On Mon, Mar 8, 2010 at 11:28, Stephen Tetley
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
I don't see where it's different from my code (i.e. modulo the ordering of 'Either ...'). Now I may be missing something, but defining a conversion function and running smallcheck (or I guess quickcheck) shows that 'prop_test2EqTestM' holds (I took the liberty of swapping the Either around compared to your 'test2' above): data TestData = DataConst1 Int String | DataConst2 Int Int | DataConst3 String String | DataConst4 String Int test2 (DataConst1 x _) = Right x test2 (DataConst2 x _) = Right x test2 (DataConst3 _ y) = Left y test2 (DataConst4 _ y) = Right y type TestDataM = (Either String Int, Either String Int) testM :: TestDataM -> Either String Int testM (x@(Right _), _) = x testM (Left _, y) = y conv2Tuple :: TestDataM -> TestData conv2Tuple (Right x, Left y) = (DataConst1 x y) conv2Tuple (Right x, Right y) = (DataConst2 x y) conv2Tuple (Left x, Left y) = (DataConst3 x y) conv2Tuple (Left x, Right y) = (DataConst4 x y) prop_test2EqTestM tdm = let td = conv2Tuple tdm in test2 td == testM tdm What am I missing? /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus@therning.org Jabber: magnus@therning.org http://therning.org/magnus identi.ca|twitter: magthe

On 8 March 2010 12:06, Magnus Therning
What am I missing?
Hi Magnus I was restating what Jonas mentioned but didn't elaborate - that Joe's original program had a type error. The solutions posted did two things: 1. Rewrite Joe's original (algebraic) data type to encode it with products (aka pair - (,) ) and sums (Either) - this allowed some of the pattern matching to be factored, reducing the number of cases in the function test from 4 to 2 2. Correct the answer type to use a proper sum (Either Int String), where previously some of declarations of test returned Int others String (and so wouldn't type check). Strictly speaking your program wasn't "equivalent" to the original because it did step 2 and would actually work! Best wishes Stephen
participants (4)
-
Joe Fox
-
Jonas Almström Duregård
-
Magnus Therning
-
Stephen Tetley