[Haskell-beginners] Help in pattern matching

Magnus Therning magnus at therning.org
Mon Mar 8 05:21:32 EST 2010


On Mon, Mar 8, 2010 at 09:11, Joe Fox <fox.joe87 at gmail.com> wrote:
> 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


More information about the Beginners mailing list