Thanks Stepen <br><br>will try out factoring<br><br><div class="gmail_quote">On Mon, Mar 8, 2010 at 7:44 PM, Stephen Tetley <span dir="ltr"><<a href="mailto:stephen.tetley@gmail.com">stephen.tetley@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">Hi Joe - I've cc'ed back to list...<br>
<br>
I'm supposing from what you've said that only DataConst1 & DataConst2<br>
have ids - so I've made a type synonym VId<br>
<br>
<br>
type VId = Int<br>
<br>
data TestData = DataConst1 VId String<br>
| DataConst2 VId Int<br>
<div class="im"> | DataConst3 Sting String<br>
| DataConst4 String Int<br>
<br>
<br>
</div>If both DataConst1 & DataConst2 have VId's the you right - you have to<br>
pattern match for both:<br>
<br>
setVid_to_4 :: TestData -> TestData<br>
setVid_to_4 (DataConst1 _ y) = DataConst1 4 y<br>
setVid_to_4 (DataConst2 _ y) = DataConst2 4 y<br>
setVid_to_4 var = var<br>
<br>
<br>
If all the cases of TestData had a VId e.g.:<br>
<br>
<br>
data TestData = DataConst1 VId String<br>
| DataConst2 VId Int<br>
| DataConst3 VId Sting String<br>
| DataConst4 Vid String Int<br>
<br>
... then you could factor out VId to get these two data types<br>
<br>
<br>
data FactoredTD = FactoredTD VId TD<br>
<br>
data TD = DC1 String<br>
| DC2 Int<br>
| DC3 String String<br>
| DC4 String Int<br>
<br>
setVid_to_4 :: FactoredTD -> FactoredTD<br>
setVid_to_4 (FactoredTD _ y) = FactoredTD 4 y<br>
<br>
<br>
This factoring transformation is quite common.<br>
<br>
For your particular data type you could factor the other way:<br>
<br>
<br>
data TestDataX = DataConst1_or_2 VId (Either String Int)<br>
<div class="im"> | DataConst3 Sting String<br>
| DataConst4 String Int<br>
<br>
<br>
</div>setVid_to_4 :: TestDataX -> TestDataX<br>
setVid_to_4 (DataConst1_or_2 _ y) = DataConst1_or_2 4 y<br>
setVid_to_4 y = y<br>
<br>
However this factoring is quite horrible - I can't think of anywhere<br>
where I've seen it.<br>
<br>
Generally I wouldn't be too concerned about redundancy in pattern<br>
matching. If its easy to consistently name all the constructors in an<br>
algebraic data type, then that's a strong indication that you've put<br>
it into a good form.<br>
<br>
If you're still taxed by duplicate pattern matching you can then write<br>
projection and modification functions to do it once and once only:<br>
<br>
<br>
-- Has to return a Maybe as DC3 and DC4 have no ID<br>
getVId :: TestData -> Maybe VId<br>
getVid (DataConst1 iden _) = Just iden<br>
getVid (DataConst2 iden _) = Just iden<br>
getVid _ = Nothing<br>
<br>
<br>
-- note - uses an update function that accesses the original value<br>
-- rather than a simple replace<br>
--<br>
updateVid :: (Vid -> Vid) -> TestData -> TestData<br>
updateVid f (DataConst1 iden y) = DataConst1 (f iden) y<br>
updateVid f (DataConst2 iden y) = DataConst2 (f iden) y<br>
updateVid f var = var<br>
<br>
incrementVid :: TestData -> TestData<br>
incrementVid = updateVid (\x -> x+1)<br>
<br>
Best wishes<br>
<font color="#888888"><br>
Stephen<br>
</font><div><div></div><div class="h5"><br>
On 8 March 2010 12:19, Joe Fox <<a href="mailto:fox.joe87@gmail.com">fox.joe87@gmail.com</a>> wrote:<br>
> Hi Guys,<br>
><br>
> First thanks for all yours inputs.<br>
><br>
> Actually test is just a simple function , actually my actual function is<br>
> pretty complex and huge ...<br>
><br>
> let me redefine Test function , i guess i took a bad example which created<br>
> lot of confusion<br>
><br>
> the type of the test functions will be something like this , (i don't have<br>
> any ambiguity on type of the function, )<br>
><br>
> test:: TestData -> TestData<br>
><br>
> My question is like is there any way i can match multiple pattern in a<br>
> function statement .<br>
><br>
> like this (I know this doesnt work , but just trying to explain whats on my<br>
> mind)<br>
><br>
><br>
> test var@(DataConst1 x y) == var@(DataConst2 x y) = var{id=4} -- let's say<br>
> id is the record holder for first record<br>
> test var@(_) = var --- any for other two its returns the same<br>
><br>
> the working code would be like<br>
> test var@(DataConst1 x y) = var{id=4}<br>
> test var@(DataConst2 x y) = var{id=4} --- I want these two lines in one<br>
> line, because the logic is same<br>
> test var@(_) = var<br>
><br>
> some thing of this sort.<br>
> I hope i didnt add to the confusion<br>
><br>
><br>
> Thanks<br>
> Joe<br>
><br>
</div></div></blockquote></div><br>