0

我想查看一个列表是否包含在另一个列表中,并从列表列表中提取该列表,例如,我想查看是否["bc","abc"]包含在其中,[["a","b","c","a","b","c","de"],["a","bc","abc","de"],["a","bc","abc","d","e"],....]并且我想制作一个包含该特定列表的列表,例如[["a","bc","abc","de"],["a","bc","abc","d","e"]]. 因为[["aabbcc","aacc"]]我只想匹配这样的东西["cc","aabbcc","aacc"]or["c","c","aabbcc","aacc"]但不匹配[["ccaabbcc","aacc"]]。有人能帮我吗?

4

3 回答 3

4

据我了解,您想要一些关心列表中元素顺序的东西

如果您确实关心元素的顺序:

import Data.List (subsequences)

included :: Eq a => [a] -> [[a]] -> [[a]]
included ls nest = filter (\x -> any (ls==) $ subsequences x) nest

测试:

*Main> included [1,2,3,4] [[1,3,4,5,2],[1,2,4,5],[1,2,3,4,5]]
[[1,2,3,4,5]]

如果您不关心元素的顺序:

import Data.List (subsequences, permutations, concat)

included :: Eq a => [a] -> [[a]] -> [[a]]
included ls nest = filter func nest
  where
    func x = any (ls==) $ concat $ map subsequences $ permutations x

测试:

*Main> included [1,2,3,4] [[1,3,4,5,2],[1,2,4,5],[1,2,3,4,5]]
[[1,3,4,5,2],[1,2,3,4,5]]
于 2013-04-28T20:39:05.910 回答
3

假设我在评论中的问题的答案是否定的,

import Data.List

g :: (Eq a) => [a] -> [[a]] -> [[a]]
g a xs = [x | x <- xs, 
              or [and . map (uncurry (==)) $ zip a y | y <- init $ tails x]]

测试:

Prelude Data.List> g ["bc","abc"] [["a","bc","a","abc","de"],["a","bc","abc","de
"],["a","bc","abc","d","e"]]
[["a","bc","abc","de"],["a","bc","abc","d","e"]]
于 2013-04-28T20:32:24.370 回答
2

假设评论中威尔问题的答案是“否”:

import Data.List (isInfixOf)

foo :: Eq a => [a] -> [[a]] -> [[a]]
foo needle haystack = filter (needle `isInfixOf`) haystack

(未经测试。)

于 2013-04-28T20:36:44.213 回答