Haskell-非穷举模式,以防万一 [英] Haskell - Non-exhaustive patterns in case

查看:140
本文介绍了Haskell-非穷举模式,以防万一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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):万一情况不尽人意

Melon.hs:(172,10)-(182,74): Non-exhaustive patterns in case

其中表示行的数字是从"= case F'(pc:pcs)(readC pc核心)核心"到"in FALL core"(((name,pcs'):acc)ps)中的行数"

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"

我认为问题在于(pc:pcs)的模式用尽,但我只是不明白如何解决.

I think the problem is in exhausting the pattern for (pc : pcs) but I just cannot understand how I can solve it.

任何帮助将不胜感激.

此代码已更新:

我写了以下内容:

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

但是程序只是陷入无限循环:S

but the program just falls into an infinite loop :S

推荐答案

非穷尽模式"表示您有一组模式匹配项,不能涵盖所有可能的组合.在您的代码中,您遇到以下情况:

"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.

您同时处理了Maybe部分的两种模式,并且该对只有一个模式,但是仅在一个单元素列表中匹配,因此对于看起来像这样的模式将失败Just (core', [])Just (core', (pc' : pcs')).

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')).

通常最好处理所有可能的情况(即具有详尽的模式匹配),即使您希望某些情况永远不会发生.如果您确实非常确定不可能发生这种情况,请使用error "this will never happen because blah blah blah"之类的东西.如果您无法解释为什么它永远不会发生,那么您应该考虑正确处理它. :]

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. :]

这篇关于Haskell-非穷举模式,以防万一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆