正如主要答案所示,列表concat是罪犯。呼喚「DOALL」,與列表作爲輸入...將導致ISEQ:
;;insertion sort helper
(defn insert [s k]
;;find the insert point
(let [spl (split-with #(< % k) s)
ret (concat (first spl) (list k) (last spl))]
(doall ret)))
;;insertion sort
(defn insert-sort [s]
(reduce (fn [s k] (insert s k)) '() s))
別急......是序列還是懶惰?
上面的代碼有趣的是,下面的代碼表明這個序列確實還是很懶!
;;insertion sort helper
(defn insert [s k]
;;find the insert point
(let [spl (split-with #(< % k) s)
ret (concat (first spl) (list k) (last spl))
ret2 (doall ret)
_ (println "final " (.getClass ret2))]
ret2))
;;insertion sort
(defn insert-sort [s]
(reduce (fn [s k] (insert s k)) '() s))
所以,如果名單仍然懶惰,那麼爲什麼不使用的doall的修復什麼?
「doall」函數不適合返回「非懶惰」列表,而是它確保它返回的列表將通過完整遍歷進行評估。
因此,問題的實質是多重函數調用,懶惰無疑與原始問題中代碼的這一方面有關,但它不是溢出的「主要」來源。
有趣的是我的repl它早在列表大小891處去掉 – jayunit100 2012-03-25 23:46:13