合并嵌套在列表列表[Haskell]中的元组



我正在尝试合并:

[[('a',False),('b',False)]]
with
[[('a',True)],[('b',True)]]

并获得以下内容:

[[(a,True),(b,True),(a,False),('b',False)]]

Essiently将两个列表元组合并为一个。

我试图创建一个函数来实现这一点,但我没有得到我想要的输出。这是我当前的函数和它给我的输出

mergeFunc :: [[a]] -> [[a]] -> [[a]]
mergeFunc xs [] = xs
mergeFunc [] ys = ys
mergeFunc (x:xs) ys = x : mergeFunc ys xs
[[('a',True),('b',True)],[('a',False)],[('b',False)]]

看起来我正在合并一个比我想要的更高的级别,但我不知道如何解决这个问题。

您可以使用concat :: Foldable f => f [a] -> [a]:将子列表连接在一个列表中

mergeFunc :: [[a]] -> [[a]] -> [[a]]
mergeFunc xs ys = [concat xs ++ concat ys]

这将产生:

ghci> mergeFunc [[('a',True)],[('b',True)]] [[('a',False),('b',False)]]
[[('a',True),('b',True),('a',False),('b',False)]]

但是,将列表封装在单例列表中并没有多大意义。在这种情况下,将其定义为:更有意义

mergeFunc :: [[a]] -> [[a]] -> [a]
mergeFunc xs ys = concat xs ++ concat ys

因为这去除了不必要的列表级别。

最新更新