gpt4 book ai didi

haskell - "non-Exhaustive patterns in function listelem" haskell

转载 作者:行者123 更新时间:2023-12-03 14:39:09 25 4
gpt4 key购买 nike

嗨,我对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

最佳答案

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)永远不会匹配。

关于haskell - "non-Exhaustive patterns in function listelem" haskell ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19623077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com