Although Ganesh answered your exact question, I want to say that your problem simply indicates the wrong approach to the development of data types.
The following approach is much more flexible and fixes your problem as such:
data Person = Person { gender :: Gender, firstName :: String, lastName :: String } data Gender = Male | Female flipNames (Person gender firstName lastName) = Person gender lastName firstName
The rule behind this is quite simple: whenever you see that you are creating several constructors with the same fields, just use one constructor and enter another field with the enumeration type, as in the code above.
You will not lose the ability to match patterns, since patterns can be similar to Person Male firstName lastName , and you can draw conclusions such as Gender Enum and Bounded , which will undoubtedly help you with types that are not so trivial. For example:.
data Gender = Male | Female deriving (Enum, Bounded) allGenders :: [Gender] allGenders = enumFrom minBound maidenName :: Person -> Maybe String maidenName (Person Female _ z) = Just z maidenName _ = Nothing
Nikita Volkov
source share