I have defined a Haskell type similar to the following:
data TypeData = TypeA Int | TypeB String | TypeC Char deriving (Eq, Show)
At some point, I need a way to filter a [TypeData]
for all non-TypeC instances. The signature of the function I am trying to write is:
-- Returns a tuple containing (TypeC elements, non-TypeC elements)
partitionTypeCs :: [TypeData] -> ([TypeData],[TypeData])
The partition
function seemed suitable for this:
-- Attempt:
partitionTypeCs data = partition (TypeData -> Bool) data
However, I cannot figure out what function would match the type signature TypeData -> Bool
. It looks like I need a function that can determine if a type instance is of a specific instance. I know I can use pattern matching for that by writing another function (isTypeC (TypeC _) = True
), but is there a more generic way or an lineline way of matching type instances?
I would do it like this:
I must admit that I don't understand why you don't like this. You can't compare against a constructor using equality because there is an unknown parameter involved. Pattern matching does exactly what must be done here.