gpt4 book ai didi

haskell - Haskell 守卫/模式匹配什么时候被认为是不详尽的?

转载 作者:行者123 更新时间:2023-12-02 15:49:29 27 4
gpt4 key购买 nike

<分区>

我有以下代码,它定义了一个名为 merge 的函数:

merge :: Ord a => [a] -> [a] -> [a]
merge [] [] = []
merge [] ys = ys
merge xs [] = xs
merge x y | head x <= head y = x ++ y
| head x > head y = y ++ x
merge x (y:ys) | head x <= y = x ++ merge [] (y:ys)
| head x > y = y: merge x ys
merge (x:xs) y | x <= head y = x: merge xs y
| x > head y = y ++ merge (x:xs) []
merge (x:xs) (y:ys) | x <= y = x : merge xs (y:ys)
| x > y = y : merge (x:xs) ys

这给了我以下警告:

Patterns not matched:
[_] [_]
[_] (_:_:_)
(_:_:_) [_]
(_:_:_) (_:_:_)

查看我的代码,我不清楚我遗漏了哪些模式。我应该定义哪些额外的模式来消除此警告?

顺便说一句,这是相同的功能,但使用条件语句实现,因此不会产生警告。模式匹配和保护函数与条件函数有什么区别?

merge (x:xs) (y:ys) = if x <= y then x : merge xs (y:ys) else y : merge (x:xs) ys

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