5

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

我认为我非常接近于破解坚果(虽然我真的不知道,因为我处于跟踪错误阶段),但是当我运行 listelem2 时,我遇到了一个非详尽的问题,它在 listelem 上抱怨。谁能看到我错过了什么?谢谢你提供的所有帮助!: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
4

2 回答 2

13

Emil 是对的,您错过了这两种模式,但您可以通过以下方式自己找到那些缺失的模式:

如果您运行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)永远不会匹配。

于 2013-10-27T20:57:15.273 回答
4

您没有这些调用的模式:

  • listelem [] y
  • listelem2 [] (y:ys)
于 2013-10-27T20:24:00.963 回答