【问题标题】:"non-Exhaustive patterns in function listelem" haskell“函数 listelem 中的非详尽模式”haskell
【发布时间】:2013-10-28 04:57:15
【问题描述】:

嗨,我对 haskell 很陌生。我正在尝试编写一个代码,允许我输入坐标系统列表(CS1)(即坐标列表)和所有不允许的坐标列表(CL)。该函数的目的是丢弃 CS1 中包含 CL 中这些坐标中的至少 1 个的所有坐标系,最终得到来自 CS1 的坐标系 (CS2) 的较小子集。 (混淆?对不起)

我认为我非常接近解决问题(虽然我真的不知道,因为我处于跟踪错误阶段),但是当我运行 listelem2 时我遇到了一个非详尽的问题,它抱怨列表项。谁能看到我错过了什么?谢谢你提供的所有帮助! :D

listelem0 :: (Eq a) => [a] -> a -> [a]
listelem0 [] y = []
listelem0 (x:xs) y 
            | x == y = []
            | x /= y = [x] ++ listelem0 xs y

listelem :: (Eq a) => [a] -> a -> [a]
listelem (x:xs) y
      | length (listelem0 (x:xs) y) < length (x:xs) = []
      | otherwise = listelem0 (x:xs) y

listelem1 :: (Eq a) => [[a]] -> a -> [[a]]
listelem1 [] y = []
listelem1 (x:xs) y = [listelem x y] ++ listelem1 xs y  

listelem2 :: (Eq a) => [[a]] -> [a] -> [[a]]
listelem2 [] [] = [[]]
listelem2 (x:xs) [] = (x:xs)
listelem2 (x:xs) (y:ys) = listelem2 (listelem1 (x:xs) y) ys

【问题讨论】:

    标签: haskell


    【解决方案1】:

    Emil 是对的,您缺少这 2 个模式,但您可以通过以下方式自己找到这些缺少的模式:

    如果您运行ghci -Wall yourFile.hs(或ghci -fwarn-incomplete-patterns yourFile.hs),您将看到所有不完整的模式:

    [1 of 1] Compiling Main             ( /tmp/ls.hs, interpreted )
    
    /tmp/ls.hs:2:1:
        Warning: Pattern match(es) are non-exhaustive
                 In an equation for `listelem0': Patterns not matched: (_ : _) _
    
    /tmp/ls.hs:8:1:
        Warning: Pattern match(es) are non-exhaustive
                 In an equation for `listelem': Patterns not matched: [] _
    
    /tmp/ls.hs:17:1:
        Warning: Pattern match(es) are non-exhaustive
                 In an equation for `listelem2': Patterns not matched: [] (_ : _)
    Ok, modules loaded: Main.
    

    我们以最后一个为例:In an equation for listelem2': Patterns not matched: [] (_ : _)——这意味着它听起来像:模式listelem2 [] (something:somethingElse) 永远不会匹配。

    【讨论】:

    • 有没有办法在 Prelude 中运行这些命令?看来我需要退出 ghci 并重新输入它才能运行这些命令。
    【解决方案2】:

    您没有这些调用的模式:

    • listelem [] y
    • listelem2 [] (y:ys)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多