If I have defined a datatype with, let's say, 5 attributes. How would I be able to create a list of one of the attributes.
Ex :
data Person = Person name fname sexe age height
Marie, John, Jessie :: Person
Marie = Person "Marie" _ _ _
John = Person "John" _ _ _
Jessie = Person "Jessie" _ _ _
How can I return a list containing all the names : (Marie
, John
, Jessie
)
Your code isn't valid Haskell. You could have
Then you could create a list containing the three values
marie
,john
, andjessie
withNow you can operate on this list using many built in functions like
map
andfilter
:Now if you load this into GHCi you can test this as
Some things to note:
data TypeName = ConstructorName <FieldTypes>
type AliasName = ExistingType
You can
map
over the list with a function that extracts the name:And if your data type is defined as a record, like this:
Then you can simply say
map personName people
.