Haskell错误:“非穷尽模式" [英] Haskell error: "non-exhaustive patterns"

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

问题描述

所以我有这个功能,当我尝试像这样使用它时: mergeSortedLists [1,1] [1,1] 它给了我一个错误:

So i have this function and when i try to use it like this: mergeSortedLists [1,1] [1,1] it gives me an error:

[1,1 ***例外:SortFunctions.hs:(86,1)-(91,89):不详尽 功能mergeSortedLists中的模式

[1,1*** Exception: SortFunctions.hs:(86,1)-(91,89): Non-exhaustive patterns in function mergeSortedLists

85 mergeSortedLists :: (Ord t)       => [t] -> [t] -> [t]
86 mergeSortedLists [] []            = []
87 mergeSortedLists (x:[]) []        = x:[]
88 mergeSortedLists [] (y:[])        = y:[] 
89 mergeSortedLists (x:[]) (y:[])    = (max x y) : (min x y) : []
90 mergeSortedLists (x:tail1) (y:tail2) | x > y  = x : (mergeSortedLists tail1     (y:tail2))
91                                      | otherwise = y : (mergeSortedLists (x:tail1) tail2)

我想不出问题的根源,因为我认为我已经涵盖了所有可能的情况. 这可能是什么问题?

I can't figure out the source of a problem since i think i covered every case possible. What could be the problem here?

推荐答案

您在第二和第三种情况下的模式涵盖了长度为1的列表与一个空列表合并,但是没有内容涵盖了更长的列表与该空列表合并.也就是说,您没有涵盖以下情况:

Your patterns for the second and third cases cover lists of length 1 merged with an empty list, but nothing covers longer lists merged with the empty list. That is, you didn't cover cases like this:

mergeSortedLists [3, 2, 1] []
mergeSortedLists [] [3, 2, 1]

以下是我想在更少的情况下尝试执行的功能:

Here's a function that does what I think you are trying to do in fewer cases:

mergeSortedLists :: (Ord t) => [t] -> [t] -> [t]
mergeSortedLists x [] = x
mergeSortedLists [] y = y
mergeSortedLists (x:tail1) (y:tail2)
    | x > y     = x : (mergeSortedLists tail1 (y:tail2))
    | otherwise = y : (mergeSortedLists (x:tail1) tail2)

(而且,您的函数不是从技术上合并反向排序的列表吗?)

(Also, isn't your function technically merging reverse sorted lists?)

这篇关于Haskell错误:“非穷尽模式"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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