小编dan*_*igb的帖子

clojure - (另一个)StackOverflow with loop/recur

我知道这是一个反复出现的问题(这里,这里,等等),我知道这个问题与创建延迟序列有关,但我不明白它为什么会失败.

问题:我编写了一个(不是很好的)quicksort算法来排序使用loop/recur的字符串.但是应用于10000个元素,我得到一个StackOverflowError:

(defn qsort [list]
  (loop [[current & todo :as all] [list] sorted []]
    (cond 
       (nil? current) sorted 
       (or (nil? (seq current)) (= (count current) 1)) (recur todo (concat sorted current))
       :else (let [[pivot & rest] current
                  pred #(> (compare pivot %) 0)
                  lt (filter pred rest)
                  gte (remove pred rest)
                  work (list* lt [pivot] gte todo)] 
                (recur work sorted)))))
Run Code Online (Sandbox Code Playgroud)

我用这种方式:

(defn tlfnum [] (str/join (repeatedly 10 #(rand-int 10))))
(defn tlfbook [n] (repeatedly n …
Run Code Online (Sandbox Code Playgroud)

recursion clojure

4
推荐指数
1
解决办法
382
查看次数

标签 统计

clojure ×1

recursion ×1