Ale*_*har 3 haskell pattern-matching
所以我有这个功能,当我尝试使用它时:mergeSortedLists [1,1] [1,1]它给了我一个错误:
[1,1***例外:SortFunctions.hs:(86,1) - (91,89):函数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)
Run Code Online (Sandbox Code Playgroud)
我无法找出问题的根源,因为我认为我已经涵盖了所有可能的情况.这可能是什么问题?
第二种和第三种情况的模式涵盖了与空列表合并的长度为1的列表,但没有任何内容涵盖与空列表合并的较长列表.也就是说,你没有涵盖这样的情况:
mergeSortedLists [3, 2, 1] []
mergeSortedLists [] [3, 2, 1]
Run Code Online (Sandbox Code Playgroud)
这是一个功能,可以在较少的情况下执行我认为您尝试执行的操作:
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)
Run Code Online (Sandbox Code Playgroud)
(另外,您的功能在技术上是不是合并反向排序列表?)