Haskell中的groupBy函数

Cha*_*upa 1 haskell

我知道Data.List模块带有预定义的函数groupBy,我想用它将一个字符串拆分成连续的元音和非元音的组.函数groupBy的格式如下:

groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
Run Code Online (Sandbox Code Playgroud)

如何使用此格式对字符串进行拆分?谢谢

ran*_*ame 6

像这样

ghci> import Data.Char
ghci> import Data.List
ghci> groupBy (const isAlphaNum) "A bunch of words and numbers34"
["A"," bunch"," of"," words"," and"," numbers34"]
Run Code Online (Sandbox Code Playgroud)

要么

ghci> groupBy (const isAlpha) "A bunch of words and numbers34"
["A"," bunch"," of"," words"," and"," numbers","3","4"]
Run Code Online (Sandbox Code Playgroud)

编辑:由于没有迹象表明已找到扩展问题的解决方案,为了保持SO的标准,我将完成问题的答案:

import Data.List

isVowel :: Char -> Bool
isVowel c = c `elem` "aeiouy"

bothVowelConsonant :: Char -> Char -> Bool
bothVowelConsonant a b = all isVowel [a,b] || not (any isVowel [a,b])

splitByVowel :: String -> [String]
splitByVowel s = groupBy bothVowelConsonant s
Run Code Online (Sandbox Code Playgroud)

  • @Chalupa Stackoverflow是一个问答网站,我们不会为你做功课.你有没有考虑过我的例子,用一个不同的函数替换``isAlpha``? (4认同)