我有一個簡單的clojure中的素數計算器(一種低效的算法,但我只是試圖瞭解現在的循環的行爲)。代碼是:在clojure中使用循環時溢出
(defn divisible [x,y] (= 0 (mod x y)))
(defn naive-primes [primes candidates]
(if (seq candidates)
(recur (conj primes (first candidates))
(remove (fn [x] (divisible x (first candidates))) candidates))
primes)
)
只要我不想找到太多的數字,這個工作。例如
(print (sort (naive-primes [] (range 2 2000))))
的作品。對於需要更多遞歸的任何事情,我會遇到溢出錯誤。
(print (sort (naive-primes [] (range 2 20000))))
不起作用。一般來說,無論我是否使用復發或稱爲天真素數而沒有嘗試TCO,似乎都沒有任何區別。爲什麼我在使用重複發生時遇到大型遞歸錯誤?
是否需要循環才能獲得尾遞歸?我在你的代碼中看不到循環。我會做出這個答案,但我仍然在學習Clojure。 – octopusgrabbus 2012-02-08 22:42:01
你的代碼適用於Clojure 1.2.1和1.3。我發現唯一的錯誤是當發現質量高達200,000的時候出現'OutOfMemoryError'。 – 2012-02-08 23:02:54
@octopusgrabbus,no,recur也可以以這種方式使用(就在一個函數體內)。請參閱http://clojure.org/special_forms#recur。 – 2012-02-08 23:03:38