我試圖理解,後面兩種算法有什麼區別。合併Haskell中的兩個排序列表 - 這些算法有什麼區別
第一個(合併)效果很好,但是當我嘗試將算法轉換爲守護符號(合併)時,我得到了「非窮舉模式...」異常。
但爲什麼第三個版本(合併'')工作?它與合併幾乎相同,必須有我不明白的列表(x:xs)。
1 -- merges two sortedists
2 merge xs [] = xs
3 merge [] ys = ys
4 merge (x:xs) ys
5 | x <= head ys = x : (merge xs ys)
6 | otherwise = head ys : (merge (x:xs) (tail ys))
7
8 -- merges two sorted lists (fails with exception)
9 merge' [email protected](x:xs) ys
10 | ys == [] = z
11 | z == [] = ys
12 | x <= head ys = x : (merge' xs ys)
13 | otherwise = head ys : (merge' (x:xs) (tail ys))
14
15
16 -- merges two sorted lists (exept the last element of the first list)
17 merge'' [email protected](x:xs) ys
18 | ys == [] = z
19 | xs == [] = ys
20 | x <= head ys = x : (merge'' xs ys)
21 | otherwise = head ys : (merge'' (x:xs) (tail ys))