如何显示列表但没有特定项目。

时间:2015-03-12 15:36:58

标签: list haskell

我有以下列表,其中包含字符串列表.. 我已经在原始列表中搜索了包含字符串" tom"的列表。并得到以下列表

[["leo", "tom"], ["meg", "tom"], ["George", "john", "adam", "tom"] ]

我现在希望在没有" tom"的情况下显示此列表,我会通过列表理解来完成此操作,但我不知道如何为包含列表的列表执行此操作?有人能帮助我走向正确的方向吗?

2 个答案:

答案 0 :(得分:1)

我认为将此作为列表理解来写会变得复杂。更容易链接简单的功能。

-- Intended as l `without` x
without :: Eq a => [a] -> a -> [a]
without l x = filter (/= x) l

containing :: Eq a => [[a]] -> a -> [[a]]
containing l x = filter (x `elem`) l

listsWithTom = lists `containing` "tom"
listsMinusTom = map (`without` "tom") listsWithTom

答案 1 :(得分:1)

notom xss = [[ x | x <- xs, x /= "tom"] | xs <- xss]

或者

notom = map (filter (/= "tom"))

或者在您的特定情况下

notom = map init