Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List Haskell将元组列表映射到元组列表_List_Haskell_Dictionary_Tuples - Fatal编程技术网

List Haskell将元组列表映射到元组列表

List Haskell将元组列表映射到元组列表,list,haskell,dictionary,tuples,List,Haskell,Dictionary,Tuples,我正在尝试将一个元组列表映射到另一个元组列表,但运气不好 输入示例: a = [("eo","th"),("or","he")] 示例输出: [('e','t'),('o','h'),('o','h'),('r','e')] 我试过: map (\(a,b) -> (a!!0,b!!0):(a!!1,b!!1):[]) a 但它产生了: [[('e','t'),('o','h')],[('o','h'),('r','e')]] 您必须对结果使用concat,或者使用concatMa

我正在尝试将一个元组列表映射到另一个元组列表,但运气不好

输入示例:

a = [("eo","th"),("or","he")]
示例输出:

[('e','t'),('o','h'),('o','h'),('r','e')]
我试过:

map (\(a,b) -> (a!!0,b!!0):(a!!1,b!!1):[]) a
但它产生了:

[[('e','t'),('o','h')],[('o','h'),('r','e')]]

您必须对结果使用
concat
,或者使用
concatMap
而不是
map
。毕竟,您在地图中返回列表,从而获得列表列表列表


让我们为函数指定名称和类型:

magic :: [([Char], [Char])] -> [(Char, Char)]
现在,我们可以将其视为一个两步过程:从原始列表中的每一对,我们将得到一个列表:

magicPair :: ([Char], [Char]) -> [(Char, Char)]
magicPair (a,b) = zip a b
现在,我们需要将
magicPair
映射到原始列表中的所有元素,并连接结果:

magic xs = concat (map magicPair xs)
组合
concat。map f
非常常见,因此有一个名为
concatMap
的函数用于:

magic xs = concatMap magicPair xs
对一对而不是两个参数使用函数
f
,也很常见,因此
magicPair=uncurry-zip

magic xs = concatMap (uncurry zip) xs
现在,我们可以删除两侧的
xs
,最终得到
magic
的最终变体:

magic = concatMap (uncurry zip)

这里有一个快速的方法来给出输出

simplify = (>>= uncurry zip)

这对于任何语言初学者来说都是完全模糊的。@Zeta-。。。。这将诱使他们想更多地了解哈斯克尔有多酷。