我正在尝试实现 Haskell 的标准单词功能。我正在使用 State Monad 来解决问题。
我的代码是:
type WorS = ([String],String,String)
words' :: State WorS [String]
words' = do
(lwords, words, x:xs) <- get
case x:xs of
(' ':xs) -> (put (words:lwords, [], xs) >> words')
([]) -> return lwords
(_:xs)-> (put (lwords, words ++ [x], xs) >> words')
run_word' :: String ->[String]
run_word' x = reverse $ fst (runState words' ([], [], x))
当我做:
run_word' "guns and roses"
我收到此错误:
Exception: Pattern match failure in do expression
代码在 ghci 中加载,没有任何错误。我究竟做错了什么?