2013-02-15 80 views
0

我想实现Ha​​skell的标准字功能。我正在使用State Monad来解决问题。异常:模式匹配失败Haskell

我的代码是:

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中没有任何错误。我究竟做错了什么?

+0

的[在功能上非详尽模式(可能重复http://stackoverflow.com/questions/8435575/non -exhaustive-patterns-in-function) – 2013-02-15 20:54:39

回答

4
 (lwords,words,x:xs)<-get 

x:xs列表匹配与至少一个元素(x成为第一个元素,xs成为列表的其余部分),所以你得到一个模式匹配失败时,元组的第三个成员是[]

解决方法:更换

 (lwords,words,x:xs)<-get 
     case x:xs of 

 (lwords,words,xs)<-get 
     case xs of 

(并考虑后面的功能使用不同的变量名:它会混淆当你有相同名称的两个或多个变量。)

编辑:如果您打开警告(将-Wall标志传递给ghc/ghci),您将g并在编译时警告说,该模式可能无法在运行时匹配。 (您还可以得到一个关于有一个xs可变躲在另一个xs变量,称为阴影警告。)

+0

我没有得到代码中的错误。你能否详细说明一下。 – 2013-02-15 19:44:57

+1

每次迭代循环时,状态的第三个元素中的列表变短。当它变成空白列表时,你会发现模式匹配失败。 – dave4420 2013-02-15 19:46:57

+0

谢谢。这解决了问题。 – 2013-02-15 20:20:49