【发布时间】:2016-10-12 06:09:34
【问题描述】:
我在基本的 Haskell 库中找到此文档:
zip :: [a] -> [b] -> [(a, b)]
zip takes two lists and returns a list of corresponding pairs. If one input list is short, excess elements of the longer list are discarded.
zip3 :: [a] -> [b] -> [c] -> [(a, b, c)]
zip3 takes three lists and returns a list of triples, analogous to zip.
zip4 :: [a] -> [b] -> [c] -> [d] -> [(a, b, c, d)]
The zip4 function takes four lists and returns a list of quadruples, analogous to zip.
[...snip...]
unzip :: [(a, b)] -> ([a], [b])
unzip transforms a list of pairs into a list of first components and a list of second components.
unzip3 :: [(a, b, c)] -> ([a], [b], [c])
The unzip3 function takes a list of triples and returns three lists, analogous to unzip.
unzip4 :: [(a, b, c, d)] -> ([a], [b], [c], [d])
The unzip4 function takes a list of quadruples and returns four lists, analogous to unzip.
...等等,直到 zip7 和 unzip7。
这是 Haskell 类型系统的基本限制吗?或者有没有办法实现一次 zip 和 unzip,以处理不同的输入配置?
【问题讨论】:
-
这里的问题是元组。 Haskell 元组被定义为不同的类型,它们没有任何共同点(类型方面),这使得通用 zipN 成为不可能。
标签: haskell