我有一個列表,例如[1,2,3,4,5],我必須複製列表中的每個元素以形成像[1,1,2,2, 3,4,4,5,5]。在列表中複製元素
作爲一個提示,我們引用了concat函數,它將列表列表變爲單列表。但要嘗試理解你如何更好地在Haskell中做這件事,我試圖手動完成。這裏是我的嘗試:
duplicate :: [a] -> [a]
duplicate [] = []
duplicate (x:xs) = x : (x:xs) duplicate xs
隨着錯誤消息:
Couldn't match expected type `((a0 -> Bool) -> [a0] -> [a0])
-> [a] -> [a]'
with actual type `[a]'
The function `x : xs' is applied to two arguments,
but its type `[a]' has none
In the second argument of `(:)', namely `(x : xs) myTakeWhile xs'
In the expression: x : (x : xs) myTakeWhile xs
Failed, modules loaded: none.
我的思維方式是你負面因素列表的頭整個列表,然後遞歸調用尾部的功能這份名單。例如,在僞代碼:
duplicate [1, 2, 3]
1 : [1, 2, 3] duplicate [2, 3]
2 : [2, 3] duplicate [3]
3: [3] duplicate []
return list [1, 1, 2, 2, 3, 3]
這是處理這個問題和我在哪裏,我試圖去錯語法的慣用方式是什麼?我不是在尋找替代的,更高效的代碼解決方案,我只是試圖習慣解決問題的功能性方式,而不是尋求事物的必要方式。
謝謝!
'x y'總是意味着'將函數'x'應用於參數'y''。在'[1,2,3]函數'中,'[1,2,3]'不是函數,因此類型錯誤。 – 2014-10-29 13:25:57
您的想法是正確的,但是您錯誤地應用了該功能。代碼,因爲它試圖查看列表作爲您通過'myTakeWhile'和列表的函數。也許,先複製尾巴,然後再追加兩次。 – 2014-10-29 13:29:05
myTakeWhile是一個複製錯誤,試圖遞歸調用重複。已編輯。 – Bradley 2014-10-29 13:42:24