[英]Converting a list of tuples to a tuple of lists recursively
I need a recursive function that returns a tuple of lists from a list of tuples. 我需要一个递归函数,它从元组列表中返回一个列表元组。 In other words, its type signature would be
换句话说,它的类型签名就是
func :: [(a,b)] -> ([a],[b])
Here is an example: 这是一个例子:
λ> func [(1,3), (2,4)]
([1,2], [3,4])
I made two functions to return a list of the first and second elements from the tuples and joining them, but I want only one recursive function to do it. 我做了两个函数来从元组返回第一个和第二个元素的列表并加入它们,但我只想要一个递归函数来完成它。
You're looking for unzip
: 你正在寻找
unzip
:
unzip :: [(a, b)] -> ([a], [b])
As general advice, when you know a type signature and you're wondering if there's any already-existing function that has that type signature, Hoogle is a good bet; 作为一般建议,当您知道类型签名并且您想知道是否存在具有该类型签名的任何已存在的函数时, Hoogle是一个不错的选择; searching for that type signature finds
unzip
as its first and only result. 搜索该类型签名会将
unzip
作为其第一个也是唯一的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.