
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? -- View this message in context: http://www.nabble.com/Pattern-match-failure-tp16600643p16600643.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

On Thu, Apr 10, 2008 at 2:05 AM, 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
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) Or: 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. Luke

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.

I think you were having a similar problem in an earlier thread. Others have given answers, but I'll try to drive the point home further. In a pattern match like this: f a = case a of [Rule x y] -> ... the pattern is *NOT* matched against each element of the list, but against the list as a whole. Say we have an expression like: let f [x] = True l = [1,2,3] in f l In the third line we attempt to match the list [1,2,3] against the pattern [x]. This cannot possibly work -- [x] can only match a list with a single element! I hope it's more clear now -- and that I answered the right question at all! There's more detail below if you're interested. {-# nitty-gritty on #-} In more detail, recall that [x] is nothing but syntactic sugar for x:[]. That is, the (:) constructor applied to x and []. [1,2,3] in turn stands for 1:2:3:[]. Pattern matching then proceeds: Match 1:2:3:[] against x:[] (:) matched -> match 1 against x; 2:3:[] against [] Match 1 against x x is a variable, bind it to 1 Match 2:3:[] against [] Match failed! {-# nitty-gritty off #-} Have fun =) Jealous 'cause I never had any homework in Haskell, -- Ariel J. Birnbaum
participants (4)
-
Ariel J. Birnbaum
-
Daniel Fischer
-
Jackm139
-
Luke Palmer