正如主要答案所示,列表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