I have my own data type:
type Types = String
data MyType = MyType [Types]
I have a utility function:
initMyType :: [Types] -> MyType
initMyType types = Mytype types
Now I create:
let a = MyType ["1A", "1B", "1C"]
How can I get the list ["1A", "1B", "1C"] from a? And in general, how can I get data from a data constructor?
Besides using pattern matching, as in arrowdodger’s answer, you can also use record syntax to define the accessor automatically:
This defines a type exactly the same as
but also defines a function
and allows (but doesn’t require) the syntax
MyType { getList = ["a", "b", "c"] }for constructing values of MyType. By the way,initMyTypesis not really necessary unless it does something else besides constructing the value, because it does exactly the same as the constructor MyType (but can’t be used in pattern matching).