3
我需要將多個列表與常用元素合併爲一個。我應該在清單內做到這一點。在Haskell中合併類似的列表清單
例如:
INPUT: [[1,2,3],[5,6],[8,3,11],[4,9,1]]
MY輸出: [[1,2,3,8,3,11,1,2,3,4,9,1],[]]
需要的輸出: [[1,2,3 ,8,11,9],[5,6]]
另一個例子:
INPUT: [[4],[0],[7,10],[6],[6]]
MY輸出: [[6,6],[]]
OUTPUT: [[4],[0],[7,10],[6]]
我的代碼:
mergeAllLists :: [[Integer]] -> [[Integer]]
mergeAllLists (x:[]) = [x]
mergeAllLists (x:[]:y) = [x]
mergeAllLists (x:y:[]:_) = mergeOneToAll_ x [y]
mergeAllLists (x:xs) = mergeAllLists (mergeOneToAll x xs)
mergeOneToAll :: [Integer] -> [[Integer]] -> [[Integer]]
mergeOneToAll _ [] = [[]]
mergeOneToAll list (y:list_of_list) = (mergeLists list y) : (mergeOneToAll list list_of_list)
列表中是否包含數字的兩次出現?如果它們是重要的?我的意思是:如果輸入是[[1,1,2],[2,3]],你會介意結果是[[1,2,3]]而不是'[[1,1 1,2,3]]'? – Bakuriu
是的,我可以刪除重複以後 – dandycomp