((fn [coll] (letfn [(add-item [acc coll idx]
(conj acc (nth coll idx)))
(add-group [acc coll]
(conj acc (create-group coll)))
(decrease-coll [coll acc]
(drop (count (last acc)) coll))
(not-group-member? [idx coll]
(not= (first coll) (nth coll idx)))
(out-of-bounds? [idx coll]
(or (empty? coll) (> idx (count coll))))
(create-group [coll] (loop [idx 0
coll coll
acc []]
(if (or (out-of-bounds? idx coll)
(not-group-member? idx coll))
acc
(recur (inc idx) coll (add-item acc coll idx)))))
(process-coll [coll] (loop [coll coll
acc []]
(if (empty? coll)
acc
(recur (decrease-coll coll acc)
(add-group acc coll)))))]
(process-coll coll))) [1 1 2 1 1 1])
Run Code Online (Sandbox Code Playgroud)
当我尝试运行这个时,我收到了
java.lang.IndexOutOfBoundsException: null
RT.java:795 clojure.lang.RT.nthFrom
RT.java:764 clojure.lang.RT.nth
/clojure/scratch-work-4clojure.clj:13 user/eval10378[fn]
/clojure/scratch-work-4clojure.clj:22 user/eval10378[fn]
/clojure/scratch-work-4clojure.clj:7 user/eval10378[fn]
/clojure/scratch-work-4clojure.clj:30 user/eval10378[fn]
/clojure/scratch-work-4clojure.clj:31 user/eval10378[fn]
/clojure/scratch-work-4clojure.clj:3 user/eval10378
Run Code Online (Sandbox Code Playgroud)
我一直试图调试这个问题一段时间了.我把它分成几个函数来试图找出导致错误的原因,但还没有确定它.任何有关导致此问题的帮助以及如何在Clojure中调试此类错误将不胜感激.
你的out-of-bounds?
支票错了.你想要> =而不是>.索引从0到n-1.