我有以下代码:
import Debug.Trace (trace)
mtrace :: Show a => String -> a -> a
mtrace msg value =
trace (msg ++ show value) value
isVowel :: Char -> Bool
isVowel = (`elem` "AEIOU")
vowelSlice :: String -> ([Maybe Char], String)
vowelSlice "" = ([], [])
vowelSlice (c:s)
| isVowel c = (Nothing:chars, c:vowels)
| otherwise = (Just c:chars, vowels)
where (chars, vowels) = vowelSlice s
stringTogether :: [Maybe Char] -> String -> String
stringTogether [] "" = ""
stringTogehter (Just c:cs) vowels = c:stringTogether cs vowels
stringTogehter (Nothing:cs) (v:vs) = v:stringTogether cs vs
process :: String -> String
process s = stringTogether (mtrace "chars: " chars) (mtrace "vowels: " cycledVowels)
where
(chars, vowels) = vowelSlice s
cycledVowels = tail vowels ++ [head vowels]
main :: IO ()
main = do
line <- getLine
putStrLn $ process line
runhaskell
为了测试,我使用命令运行我的文件,并HELLO PEOPLE
在程序运行后作为用户输入输入。我期待输出:HELLE POEPLO
或类似的东西,因为我的程序仅用于移动元音。我的程序运行良好,直到它尝试运行该stringTogether
方法。具体来说,问题在于模式匹配,我有一个数组:
[Just 'H',Nothing,Just 'L',Just 'L',Nothing,Just ' ',Just 'P',Nothing,Nothing,Just 'P',Just 'L',Nothing]
以及
(Just c:cs) vowels
我希望它匹配但不知何故它似乎不起作用的模式。当我运行代码并输入时,HELLO WORLD
我收到以下错误:
18:1-25: Non-exhaustive patterns in function stringTogether
我使用跟踪模块记录了一些内容,并且在输入stringTogether
函数之前一切看起来都符合预期
我可能忽略了一些非常明显的事情,但我无法理解为什么模式匹配不起作用,我希望有人能够提供帮助。提前致谢!