Yan Zhu
Yan Zhu

Reputation: 4346

Why am I getting a pattern-matching error, here?

I'm new to Haskell and I'm getting a patten match error, which I can't figure out:

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)

In GHCi, I got the following error message:

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.

Note that the second input argument is [String]; therefore, y should be a String. Why does GHC think it is a Char? Am I missing some subtle point here?

(I am using GHC 7.8.4.)

Upvotes: 0

Views: 87

Answers (1)

Yan Zhu
Yan Zhu

Reputation: 4346

I should change Dict [String] to Dict String. Thanks Jon for pointing it out!

Upvotes: 1

Related Questions