[Haskell-cafe] Pattern match failure

Luke Palmer lrpalmer at gmail.com
Wed Apr 9 22:19:08 EDT 2008

On Thu, Apr 10, 2008 at 2:05 AM, Jackm139 <jackmarathon at gmail.com> wrote:
>  I'm trying to write a function to recognize a context free grammar, but I
>  keep getting pattern match failure errors. This is what I have:
>  data Grammar c = Brule c c c | Rule c c
>  gez = [(Brule 'S' 'p' 'D'),(Brule 'D' 't' 'E'),(Rule 'E' 'j')]
>  recog :: String -> String -> [Grammar Char] -> Bool
>  recog a b list = case list of
>         [Brule x y z] -> if a == [x] then recog [z] b list else recog a b list
>         [Rule x y] -> True

I am stumped as to what this function is trying to do.  But your
pattern matches are not total; in particular you only handle lists of
a single element.  I suggest either:

recog a b list = case list of
    (Brule x y z : rules) -> ... (involving 'rules')
    (Rule x y : rules) -> ...    (involving 'rules')
    [] -> ... (case for the empty list)


recog a b (rule:rules) =
    case rule of
        Brule x y z -> ...
        Rule x y -> ...
recog a b [] = ...

But the point is that you have to say how to handle lists of more thn
one element.


More information about the Haskell-Cafe mailing list