Reputation: 599
I am attempting to create a function to pattern match a structure which is a tuple containing a value and a list of tuples of the form
'a * ('b * 'c) list -> 'b list
for example given the following:
let clubDetails = ("MyClub", [("Secretary", "Jill");("Captain", "Bob");("Email", "[email protected]")])
I need a function to return the list ["Secretary";"Captain";"Email"]
So I thought I could do something like this :
let getClubAttributes ca =
match ca with
| (a, [(b,c)]) -> [b]
| _ -> []
but here
getClubAttributes clubDetails
Returns the empty list. I feel like I am missing something pretty obvious.
Thanks, Russell.
Upvotes: 2
Views: 402
Reputation: 12184
The pattern match form that you have written will only match if the list
in your tuple has exactly one element.
I note that you have written a default _
case in your pattern match which returns the empty list and this is the case you are hitting. I suspect you've added this case to remove the compiler warning but the compiler warning is, in fact, warning you of this exact problem.
You do not actually need a pattern match because your data structure can be handled with only one case.
let clubDetails = ("MyClub", [("Secretary", "Jill");("Captain", "Bob");("Email", "[email protected]")])
let getClubAttributes (ca, attribList) =
List.map fst attribList
The new getClubAttributes
function simply creates a new list from the original by taking the first element of each item in the list.
Upvotes: 5