匹配元组列表中的一项

发布于 2024-10-08 07:25:14 字数 208 浏览 0 评论 0原文

我有一个 (string, int) 形式的元组列表。我正在尝试搜索列表并返回其字符串组件与参数匹配的元组,如下所示: let find_tuple string_name tuples_list =

我该怎么做?我无法完全理解它。有没有办法使用像 (string, _) ->... 这样的匹配语法?

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, _) ->...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

错々过的事 2024-10-15 07:25:14

您可以按以下方式实现此目的

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

或简单地实现

List.find (fun s -> fst s = string_name) tuples_list

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
烟凡古楼 2024-10-15 07:25:14

是的,您确实使用类似的匹配语法,但需要匹配防护(或者您可以使用 if then else)。 List 模块有一个名为 find 的函数,它将返回与谓词匹配的第一个元素。它还具有函数 filter (和 find_all - 相同的函数),返回与谓词匹配的所有元素的列表。例如:

let predicate string_name tuple = match tuple with (s, _) when s = string_name -> true
  | _ false

try
  let x = List.find (predicate "query") tuples_list in
    ...
  with Not_found -> ...

编辑:更好的谓词:

let predicate string_name (s, _) = s = string_name

但是更好的解决方案是使用 List.assoc 它适用于元组列表,并将元组视为键值对:

try
  let x = List.assoc "query" tuples_list in ...
with Not_found -> ...

虽然 < code>List.assoc 是元组的第二个元素(在您的情况下是 int )。如果您想要元组的值,请重新创建它,或使用第一种方法。

Yes, you do use matching syntax like that, but will need match guards (or you can use if then else). The List module has a function called find that will return the first element that matches a predicate. It also has the function filter (and find_all - same function) that returns a list of all the elements that match the predicate. For example:

let predicate string_name tuple = match tuple with (s, _) when s = string_name -> true
  | _ false

try
  let x = List.find (predicate "query") tuples_list in
    ...
  with Not_found -> ...

EDIT: a better predicate:

let predicate string_name (s, _) = s = string_name

However the better solution is to use List.assoc which works on lists of tuples, and considers the tuples to be key-value pairs:

try
  let x = List.assoc "query" tuples_list in ...
with Not_found -> ...

Although the return value of List.assoc is the second element of the tuple (an int in your case). If you want the value of the tuple, either recreate it, or use the first approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文