为什么我会遇到模式匹配错误?

时间:2015-01-15 01:18:42

标签: haskell pattern-matching

我是Haskell的新手,我收到了一个模式匹配错误,我无法理解:

import qualified Data.Set as S 

type Dict = S.Set


extendWordImpl :: String -> [String] -> (Dict [String], [String]) -> (Dict [String], [String])
extendWordImpl end [] res = res
extendWordImpl end (y:ss) (dd, xs) | end == y = extendWordImpl end ss (dd, y:xs)
                                   | S.member y dd = extendWordImpl end ss (S.delete y dd, y:xs)
                                   | otherwise = extendWordImpl end ss (dd, xs)

在GHCi中,我收到以下错误消息:

Prelude> :l WordLadderI.hs
[1 of 1] Compiling Main             ( WordLadderI.hs, interpreted )

WordLadderI.hs:10:49:
    Couldn't match type ‘[Char]’ with ‘Char’
    Expected type: S.Set String
      Actual type: Dict [String]
    In the second argument of ‘S.member’, namely ‘dd’
    In the expression: S.member y dd

WordLadderI.hs:10:86:
    Couldn't match type ‘Char’ with ‘[Char]’
    Expected type: [String]
      Actual type: String
    In the first argument of ‘S.delete’, namely ‘y’
    In the expression: S.delete y dd
Failed, modules loaded: none.

请注意,第二个输入参数是[String];因此,y应该是String。为什么GHC认为它是Char?我在这里错过了一些微妙的观点吗?

(我正在使用GHC 7.8.4。)

1 个答案:

答案 0 :(得分:1)

我应该将Dict [String]更改为Dict String。感谢Jon指出它!