大家好,我試圖重現哈斯克爾歸併排序,這裏是我的代碼:合併排序進入無限循環
-- merge
merge :: (Ord a) => [a] -> [a] -> [a]
merge [] [] = []
merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys)
| x <= y = x:(merge xs (y:ys))
| otherwise = y:(merge (x:xs) ys)
-- split
splitIn2 :: (Ord a) => [a] -> ([a],[a])
splitIn2 [] = ([],[])
splitIn2 xs = splitAt ((length xs `div` 2)+1) xs
-- msort
msort :: (Ord a) => [a] -> [a]
msort [] = []
msort [x] = [x]
msort (xs) = merge (msort as) (msort bs)
where (as,bs) = splitIn2 xs
它編譯於GHC,以及它的工作原理爲:
*Main> msort([])
[]
*Main> msort([1])
[1]
但是它沒有正確地完成它的工作,因爲它開始無限循環(至少這是我的想法)並且它不打印任何東西。
我認爲這是因爲我不像我在其他遞歸實驗中所做的那樣從列表中刪除元素,任何建議?
好吧,我不明白爲什麼它不工作還與+1,任何解釋? – nick
@ graphtheory92問題在於2元素列表。哈馬爾解釋了這一點。 –