0
我想實現 '交錯' 爲自己打了一個OutOfMemoryError異常Clojure的做法:交錯 - 的OutOfMemoryError
我的版本應該是一樣的:
(interleave [1 2] [3 4])
下面是代碼:
(defn myInterleav [col1 col2]
(loop [r []
c1 (first col1)
c2 (first col2)]
(if (and c1 c2)
(recur (conj (conj r c1) c2)
(next col1)
(next col2))
r)))
(myInterleav [1 2] [3 4])
版本2作爲'noisesmith'建議重新綁定參數,但得到nullPointer錯誤。
(defn myIL2 [col1 col2]
(loop [m []
[e1 & c1] col1
[e2 & c2] col2]
(if (and e1 e2)
(recur (conj m e1 e2)
c1
c2)
m
)))
對於'(myIL2 [1 2] [3 4])',我得到''[1 3 2 4]''。 – Thumbnail 2015-04-02 06:22:09
'(flatten(map list col1 col2))'會做你想做的 - 我的想法是,如果你可以在每個集合中取對應的項目,以某種方式加入它們,然後將整個集合的內容作爲一個整體,你會得到你想要的位置。幾乎肯定有更好的方法,我希望有人會指出! – peter 2015-04-02 07:46:36
@CodeFarmer這應該工作,除非false或nil在你的列表之一,我只是更新了我的答案來解決這個問題。 – noisesmith 2015-04-02 14:04:18