【发布时间】:2010-12-17 21:54:39
【问题描述】:
几乎 2 个相同的程序可以生成无限的惰性随机序列。 第一个不会崩溃。出现 OutOfMemoryError 异常的第二次崩溃。为什么?
;Return infinite lazy sequence of random numbers
(defn inf-rand[] (lazy-seq (cons (rand) (inf-rand))))
;Never returns. Burns the CPU but won't crash and lives forever.
(last (inf-rand))
但很快就发生了以下崩溃:
;Return infinite lazy sequence of random numbers
(defn inf-rand[] (lazy-seq (cons (rand) (inf-rand))))
(def r1 (inf-rand))
;Crash with "OutOfMemoryError"
(last r1)
【问题讨论】:
标签: clojure lazy-evaluation lazy-sequences