String 如何在Haskell中创建这些字符串匹配函数?
考虑以下多行字符串String 如何在Haskell中创建这些字符串匹配函数?,string,haskell,String,Haskell,考虑以下多行字符串S: apple banana berry cantelope 我正在尝试编写/定位Haskell函数,我将在本文或match和和match中调用这些函数。以下是他们应该做的一些例子: or-match S ("apple","berry") {- should return the two line string: apple banana berry -} and-match S ("apple", "berry") --should return the empty
S
:
apple
banana berry
cantelope
我正在尝试编写/定位Haskell函数,我将在本文或match
和和match
中调用这些函数。以下是他们应该做的一些例子:
or-match S ("apple","berry")
{- should return the two line string:
apple
banana berry
-}
and-match S ("apple", "berry") --should return the empty string, or something like "No matches."
and-match S ("banana" "berry") --should return a single line containing "banana berry"
在上面的示例中,我使用类型(String,String)
作为或match
和以及match
的域参数。我不确定这是最好的方法,因为我可能想使用两个以上的参数。不过,我对Haskell还是新手,不知道如何将任意长度的元组作为函数的参数
总之,这篇文章的总体问题是:如何在Haskell中生成或match
和并match
,从而使每个函数的域参数都是任意长度的元组(或允许任意长度搜索的某种类型)?您可以这样写
andMatchPred, orMatchPred :: Eq a => [a] -> [a] -> Bool
andMatchPred needles haystack = all (`elem` haystack) needles
orMatchPred needles haystack = any (`elem` haystack) needles
然后,您所需的函数可能被编写为
andMatch, orMatch :: Eq a => [a] -> [[a]] -> [[a]]
andMatch = filter . andMatchPred
orMatch = filter . orMatchPred
如果后来发现这是一个瓶颈,那么应该有很多机会来提高效率,但是简单易读的实现应该放在第一位
在ghci中快速使用,展示如何使用它:
> let s = [["apple"],["banana","berry"],["cantelope"]]
> andMatch ["apple", "berry"] s
[]
> orMatch ["apple", "berry"] s
[["apple"],["banana","berry"]]
你可以写信
andMatchPred, orMatchPred :: Eq a => [a] -> [a] -> Bool
andMatchPred needles haystack = all (`elem` haystack) needles
orMatchPred needles haystack = any (`elem` haystack) needles
然后,您所需的函数可能被编写为
andMatch, orMatch :: Eq a => [a] -> [[a]] -> [[a]]
andMatch = filter . andMatchPred
orMatch = filter . orMatchPred
如果后来发现这是一个瓶颈,那么应该有很多机会来提高效率,但是简单易读的实现应该放在第一位
在ghci中快速使用,展示如何使用它:
> let s = [["apple"],["banana","berry"],["cantelope"]]
> andMatch ["apple", "berry"] s
[]
> orMatch ["apple", "berry"] s
[["apple"],["banana","berry"]]
这样,每个函数的域参数都是任意长度的元组(或允许任意长度搜索的某种类型)
您正在寻找一个列表,特别是在本例中[String]
。元组是固定长度的,但允许在每个位置使用不同的类型,列表是同质的,这意味着每个值必须具有相同的类型,但具有任意长度
所以你要找的是类型的函数
orMatch, andMatch :: [String] -> [String] -> [String]
您需要一个必须自己编写的函数hasSubString::String->String->Bool
,但您的代码可能类似于以下伪代码:
orMatch lines searchTerms = filter (\line -> any (hasSubString line) searchTerms) lines
andMatch lines searchTerms = filter (\line -> all (hasSubString line) searchTerms) lines
在哪里
如果string
中出现substring
,则返回True
,否则返回False
。例如hasSubString“你好,世界”“世界”==True
,hasSubString“你好,世界”“独角兽”==False
这样,每个函数的域参数都是任意长度的元组(或允许任意长度搜索的某种类型)
您正在寻找一个列表,特别是在本例中[String]
。元组是固定长度的,但允许在每个位置使用不同的类型,列表是同质的,这意味着每个值必须具有相同的类型,但具有任意长度
所以你要找的是类型的函数
orMatch, andMatch :: [String] -> [String] -> [String]
您需要一个必须自己编写的函数hasSubString::String->String->Bool
,但您的代码可能类似于以下伪代码:
orMatch lines searchTerms = filter (\line -> any (hasSubString line) searchTerms) lines
andMatch lines searchTerms = filter (\line -> all (hasSubString line) searchTerms) lines
在哪里
如果
string
中出现substring
,则返回True
,否则返回False
。例如hasSubString“hello,world”“world”==True
,hasSubString“hello,world”“unicorns”==False
,hasSubString
无需自己实现;标准库将其命名为isInfixOf
,Hackage上有更高效的版本。无需自己实现hasSubString
;标准库将其命名为isInfixOf
,Hackage上有更高效的版本。