是否有一种将元组列表转换为列表的方式比我所做的要多?

发布于 2025-02-05 17:17:42 字数 472 浏览 1 评论 0原文

我的目标是将(int * int)列表转换为int List。这就是我所做的:

let tuples_list_to_list l =
  let rec aux_tuples_to_list acc l =
    match l with
    | [] -> acc
    | x :: tl -> aux_tuples_to_list (fst x :: snd x :: acc) tl in
  aux_tuples_to_list [] l

我想知道是否有一个更“优雅”的,即使这是主观的或至少更好的写我功能的方法。我正在考虑使用list.maplist.fold_left,但是我在弄清楚我可以使用这两个功能来完成我所做的事情。基本上,我认为有可能拥有一个单线,而不是我写的整个功能。

你怎么认为?谢谢

My goal here was to convert a (int * int) list to an int list. This is what I did:

let tuples_list_to_list l =
  let rec aux_tuples_to_list acc l =
    match l with
    | [] -> acc
    | x :: tl -> aux_tuples_to_list (fst x :: snd x :: acc) tl in
  aux_tuples_to_list [] l

I was wondering if there was a more "elegant" even though this is subjective or at least better, way to write my function. I was thinking about using List.map or List.fold_left but I was having issues figuring out I could use those two functions to do the same thing I did. Basically, I think it would be possible to have a one-liner instead of the whole function I wrote.

What do you think? Thanks

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

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

发布评论

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

评论(2

慕烟庭风 2025-02-12 17:17:42

此任务适用于list.concat_map(在其他某些语言中也称为“平面地图”):

(* List.concat_map : ('a -> 'b list) -> 'a list -> 'b list *)

let tuples_list_to_list l = 
  List.concat_map (fun (x, y) -> [x; y]) l

您可以使用list.fold_left,但是您必须反向最后的结果随着列表的相反顺序构建:

let tuples_list_to_list l = 
  List.rev (List.fold_left (fun acc (x, y) -> y :: x :: acc) [] l)

This task is suited for List.concat_map (also known as "flat map" in some other languages):

(* List.concat_map : ('a -> 'b list) -> 'a list -> 'b list *)

let tuples_list_to_list l = 
  List.concat_map (fun (x, y) -> [x; y]) l

You can use List.fold_left, but you'll have to reverse the result at the end as the list is constructed in reverse order:

let tuples_list_to_list l = 
  List.rev (List.fold_left (fun acc (x, y) -> y :: x :: acc) [] l)
凉月流沐 2025-02-12 17:17:42

您还可以在模式匹配中解构元组值,而不是使用fstsnd(此版本,与您的不同,给出了一个结果列表,其数字的数字与数字一样orignal):

let tuples_list_to_list l =
  let rec aux_tuples_to_list acc l =
    match l with
    | [] -> List.rev acc
    | (x, y) :: tl -> aux_tuples_to_list (y :: x :: acc) tl in
  aux_tuples_to_list [] l

另一个选项是使用list.fold_right,它避免了在末尾以不为尾部递归的成本倒转累加器列表的需要:

let tuples_list_to_list l =
  List.fold_right (fun (x, y) acc -> x :: y :: acc) l [];;

You can also deconstruct the tuple values in the pattern matching instead of using fst and snd (This version, unlike yours, gives a result list with the numbers in the same order as the orignal):

let tuples_list_to_list l =
  let rec aux_tuples_to_list acc l =
    match l with
    | [] -> List.rev acc
    | (x, y) :: tl -> aux_tuples_to_list (y :: x :: acc) tl in
  aux_tuples_to_list [] l

Yet Another Option is using List.fold_right, which avoids the need to reverse the accumulator list at the end at the cost of not being tail recursive:

let tuples_list_to_list l =
  List.fold_right (fun (x, y) acc -> x :: y :: acc) l [];;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文