gpt4 book ai didi

Clojure IndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-01 19:16:22 28 4
gpt4 key购买 nike

((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])

当我尝试运行它时,我收到

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

我已经尝试调试这个有一段时间了。我将其分解为几个函数,试图找出导致错误的原因,但目前还无法确定。如果您能提供有关导致此问题的原因以及将来如何在 Clojure 中调试此类错误的任何帮助,我们将不胜感激。

最佳答案

您的越界?检查是错误的。你想要 >= 而不是 >。索引从 0 到 n-1。

关于Clojure IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25206964/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com