0 haskell list case non-exhaustive-patterns
我有以下代码:
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)
Run Code Online (Sandbox Code Playgroud)
它编译得很好但是当我运行程序时它会出现以下错误:
Melon.hs:(172,10) - (182,74):非详尽的模式
其中表示行的数字是来自"= f'(pc:pcs)(readC pc core)核心"到FAll核心"((name,pcs'):acc)ps"的数字
我认为问题在于耗尽(pc:pcs)的模式,但我无法理解如何解决它.
任何帮助,将不胜感激.
代码已更新为:
我写了以下内容:
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
Run Code Online (Sandbox Code Playgroud)
但程序只是陷入无限循环:S
"非详尽模式"意味着您有一组模式匹配,不包括所有可能的组合.在您的代码中,您有以下情况:
case {- stuff -} of
Nothing -> -- etc.
Just (core', [pc']) -> -- etc.
Run Code Online (Sandbox Code Playgroud)
您处理该Maybe部分的两种模式,并且该对只有一个模式,但您只匹配单个元素列表,因此对于看起来像Just (core', [])或的模式,这将失败Just (core', (pc' : pcs')).
通常最好处理所有可能的情况(即,具有详尽的模式匹配),即使您预期某些情况永远不会发生.如果你真的,真的确定一个案例是不可能的,那么使用类似的东西error "this will never happen because blah blah blah".如果你无法解释为什么它永远不会发生,那么你应该考虑正确处理它.:]