0

我一直在查看示例和文档;我不知道为什么我会得到

loop requires an even number of forms in binding vector错误。

我相信问题在于循环和初始化使用指标。我只是无法弄清楚我做错了什么。

(def usage-vec-len 13)

(defn ret-usage-indicies
"Takes a billed water consumption amount, and returns a vector of the ranges."
[reading]
(let [usage-indicies [0 0 0 0 0 0 0 0 0 0 0 0 0] curr-idx (- usage-vec-len 1)]
 (loop [curr-reading reading ui usage-indicies curr-idx]
.
.
.
 (if (= remaining-reading 0)
 ui
 (recur remaining-reading (assoc ui curr-idx curr-reading) (dec curr-idx)))))))
4

1 回答 1

5

loop表单应该看起来就像一个表单let。为了清楚起见,您可以添加逗号空格:

(loop [symbol1 init1, symbol2 init2, ...] body-of-expressions)

看起来你可能想要

(loop [curr-reading reading, ui usage-indicies, curr-idx curr-idx] ...)

wherecurr-index被初始化(反弹)到curr-index表达式let中。

更好的是,您可以将您在let表单中进行的初始化向下移动到loop表单本身。它也看起来好像usage-vec-len只是countof ,所以当您也可以在循环绑定中处理它时usage-indices,无需在全局中指定它。def

(loop [curr-reading reading
       ui [0 0 0 0 0 0 0 0 0 0 0 0 0]
       curr-idx (dec (count ui))] 
   body-of-expressions)
于 2013-03-04T20:36:15.360 回答