Consider the following example:
data TestType = Free | Occupied { oc_field1 :: Int,
oc_field2 :: Int,
oc_field3 :: Int,
oc_field4 :: Int
}
type SampleTest = [TestType]
filterOccupied :: SampleTest -> SampleTest
filterOccupied test = filter (\x -> case x of
Occupied _ _ _ _ -> True
Free -> False ) test
In the above example, inside filterOccupied I have to use four _
for matching Occupied
type.
This becomes really painful when the records has more than ten fields. Is there a better way to do this ?
You can use {}
pattern instead.
filterOccupied :: SampleTest -> SampleTest
filterOccupied test = filter (\x -> case x of
Occupied {} -> True
Free -> False ) test