问题
I have got the following code:
F (S core ps) = FAll core [] ps
where
FAll core acc ((name, (pc : pcs)) : ps)
= case F' (pc : pcs) (readC pc core) core of
Nothing ->
if (length pcs) /= 0 then FAll core ((name, pcs) : acc) ps
else FAll core acc ps
Just (core', [pc']) -> let
pc'' = pc' `mod` coresize
pcs' = pcs ++ [pc'']
in FAll core' ((name, pcs') : acc) ps
stepAll core acc [] = S core (reverse acc)
It compiles fine but when I run the program it gives the following error:
Melon.hs:(172,10)-(182,74): Non-exhaustive patterns in case
where the number indicating the rows are the ones from the "= case F' (pc : pcs) (readC pc core) core of" to the "in FAll core' ((name, pcs') : acc) ps"
I think the problem is in exhausting the pattern for (pc : pcs) but I just cannot understand how I can solve it.
Any help would be appreciated.
The code has been updated with this one:
I wrote the following:
Just (core', (pc' : pcs')) -> let
pc'' = pc' `mod` coresize
pcs' = pcs' ++ [pc'']
in stepAll core' ((name, pcs') : acc) ps
Just (core', []) -> stepAll core acc ps
but the program just falls into an infinite loop :S
回答1:
"Non-exhaustive patterns" means that you have a set of pattern matches that don't cover all possible combinations. In your code, you have cases like the following:
case {- stuff -} of
Nothing -> -- etc.
Just (core', [pc']) -> -- etc.
You handle both patterns for the Maybe portion, and the pair only has one pattern, but you match only on a single-element list, so this will fail for patterns that would look like Just (core', []) or Just (core', (pc' : pcs')).
It's usually best to handle all possible cases (i.e., have exhaustive pattern matches) even when you expect that some cases will never happen. If you're really, really certain that a case is impossible, then use something like error "this will never happen because blah blah blah". If you can't explain why it'll never happen, then you should consider handling it properly. :]
来源:https://stackoverflow.com/questions/8451653/haskell-non-exhaustive-patterns-in-case