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?
score:5
I would do it like this:
partitionTypeCs = partition f where
f (TypeC _) = False
f _ = True
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.
score:3
Pattern matching is pretty much the answer here. It'd take you 3 additional lines (including type signature), so it's not like it's a long solution. You could probably write some template haskell that could do it in general, but that seems like too complicated a solution for something so simple.
isTypeC :: TypeData -> Bool
isTypeC (TypeC _) = True
isTypeC _ = False
Credit To: stackoverflow.com
Related Query
- Haskell - Match Type Instance
- In Haskell how can I match a type class with an instance of that type class?
- Creating instance of Read type class in Haskell for custom data type
- Haskell pattern match on type
- Haskell Data instance for opaque data type
- Making a data type an instance of Show in Haskell
- haskell - generate an instance for all classes except one specific type
- Check whether a type is an instance of Show in Haskell at runtime?
- Haskell -- get TypeRep from concrete type instance
- Haskell type family instance with type constraints
- Adding type constraints to the context of instance declarations in Haskell
- Haskell : Illegal type synonym family application in instance
- Making a basic Haskell type an instance of a new typeclass
- Haskell type class instance syntax
- Defining an instance for a composite type in Haskell
- How can I define an instance for a specific type application in Haskell 98?
- Couldn't match type `[]' with `IO' -- Haskell
- Haskell Type Context from instance declaration required on functions
- Haskell IO: Couldn't match expected type `IO a0' with actual type
- Haskell Error: Couldn't match expected type `Integer' against inferred type `Int'
- I think there is a type mismatch in default definition in instance Applicative Maybe in Haskell
- Haskell - Couldn't match expected type ‘b’ with actual type ‘a’
- Haskell Couldn't match expected type 'String' with actual type 'Char'
- Couldn't match expected type of Haskell function
- Haskell - Couldn't match type [] with IO
- Creating Instance of Eq for custom data type in Haskell
- instance of "Type constructed with type argument" need not be constructed with data of that type, in Haskell
- Couldn't match expected type - Haskell Code
- Haskell - Couldn't match type `[Char]' with `Char'
- Haskell defining Functor instance for an alternative Either data type
- Haskell cannot match type
- Haskell error: Couldn't match type 'a' with 'b'
- Haskell instance of `bind` for a custom type
- Haskell could not unify type instance equations
- Haskell instance of typeclass definition with type constructor
- Haskell Error - Couldn't match expected type
- haskell Couldn't match expected type `Maybe Text' with actual type `Text'
- Haskell readFile: Couldn't match expected type ‘[String]’ with actual type ‘IO String’
- Can't match class type with specific type in haskell
- Haskell Instance of Show for user defined type - constructor not in scope
- Couldn't match expected type `[([Char], a0)]' with actual type `([Char], t0)' Haskell
- Haskell type confusion; Couldn't match the expected type despite either/or relationship
- Partially applied function type (a ->) as Functor instance in Haskell
- Haskell IO Function -> Type Match Error
- Defining an instance to a restricted a type in Haskell
- Haskell Arbitrary Instance of Higher Order type
- Haskell -- bidirectional class instance type implications OR GADT existential type qualifications?
- Haskell - Couldn't match type ‘PersistEntityBackend record0’ with ‘SqlBackend’
- Haskell assert that a type will match another
- Haskell "Couldn't match expected type ‘a’ with actual type ‘[a0]’"
More Query from same tag
- Why does this cause a memory leak in the Haskell Conduit library?
- Does {-# OPTIONS_HADDOCK hide #-} have any effect on non-exported modules?
- lexical error at character '\DEL' when doing runhaskell
- Correctly Parsing a JSON Array as Custom Data Type Object
- How to show a list of values as a comma-separated list of strings in Haskell
- Haskell simplification technique and monads
- Haskell code formatting
- What's the type error in this expression `snd (True, [2,[5]])`?
- How does one call on this Haskell function?
- Summing up Haskell output lines
- How does OverloadedStrings language extension work?
- Haskell data type alias naming
- No instance for (Show a0) arising from a use of `print' The type variable `a0' is ambiguous
- Sugaring monadic code from Reader monad
- Get a random list item in Haskell
- How do I specify that I want something to be present in Haskell
- does Database.HDBC support named parameters?
- Is it possible to define new ADTs in GHCi
- Can we have any useful first order programs with infinite lists?
- Haskell . Why does my parser fail
- Check a list if sorted in ascending or descending or not sorted in haskell?
- Haskell recursion stack overflow
- How to verify if a list of strings contains only digit characters?
- Haskell install and use packages
- Concatenate two integers in haskell
- How should I organize normal and "state" versions of functions in the state monad?
- Dependencies between projects with Haskell Stack build tool -- best workflow?
- What is wrong with my data constructor import/use, when ghc suggestion is to add constructor to import list?
- Is there a name for arrows of the type a -> a (in Haskell notation) in category theory?
- Haskell error on text file as input on Mac
- AWS lambda like execution of Haskell functions
- Haskell - Append to a list inside a list of lists and return the lists of lists updated
- How do you reduce the size of GHC's type error messages?
- Aeson decode JSON object that can be either a string or an int
- Can't define instance method with data variable