[Haskell-cafe] Pattern match failure

Daniel Fischer daniel.is.fischer at web.de
Wed Apr 9 22:31:17 EDT 2008

Am Donnerstag, 10. April 2008 04:05 schrieb Jackm139:
> 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
> how can I solve this pattern matching error?

By including cases for 
1. empty lists
2. lists with more than one element.
You probably want something like
recog a b list = case list of
		(Brule x y z:rest) -> whatever
		(Rule x y:rest) -> something_else
		[]	-> True

Besides, the "else recog a b list" is a nice infinite loop.

More information about the Haskell-Cafe mailing list