我想複製列表的第n個元素,並且我對haskell的知識非常有限。 我試圖分裂名單兩個部分,然後讓第一部分的最後一個元素,只是這些部件之間粘貼:Haskell在某個位置的重複元素
dupl n (x:xs) = (take n (x:xs)) ++ ((x:xs) !! n) ++ (drop n (x:xs))
但我總是得到錯誤:
Prelude> :l f.hs
[1 of 1] Compiling Main (f.hs, interpreted)
f.hs:5:39:
Occurs check: cannot construct the infinite type: a0 = [a0]
In the first argument of `(:)', namely `x'
In the first argument of `(!!)', namely `(x : xs)'
In the first argument of `(++)', namely `((x : xs) !! n)'
Failed, modules loaded: none.
可能有人告訴我我做錯了什麼?
這是最漂亮的一個。我想,在第一個++ [x,x] ++休息中我會更好。 – luqui 2013-02-26 01:12:14
謝謝!我也很喜歡你的建議。 – 2013-02-26 13:16:14