Matches an item in the tuples list
source link: https://www.codesd.com/item/matches-an-item-in-the-tuples-list.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Matches an item in the tuples list
I have a List of tuples of the form (string, int)
. I'm trying to search through the list and return the tuple whose string component matches the parameter, as in: let find_tuple string_name tuples_list =
How can I do this? I can't quite wrap my head around it. Is there a way to use matching syntax like (string, _) ->...
?
You can achieve this as following
let rec find_tuple string_name tuples_list =
match tuples_list with
[] -> raise Not_found
|(s, i)::tl -> if s = string_name then (s, i)
else find_tuple string_name tl
or simply
List.find (fun s -> fst s = string_name) tuples_list
Related Articles
Is it possible to paste a list into a select2 field and match each item in the list?
Return of the first value of the tuples list
SQL Procedure Error - The INSERT statement contains more items than the insert list
How to extract a tuple that contains an element from the tuple list?
How can I remove items from the generic list, depending on several conditions and using linq
How to find the index of the item with the shortest list inside?
Android: Get an item from the navigation list actionbarsherlock
jQuery slips the item into the sortable list that has the initial state as hidden
How to align the text in a list item if the previous list item is higher than
Python: lists in lists - function that adds a string to the item in the parent list without affecting the list of children?
removing the last item from the navigation list
Analyze the data via `re.match` and place it in tuple / list / dictionaries
Counting the number of items in a tuple list in Erlang
Python list.remove the items in the second list
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK