octopusgrabbus
octopusgrabbus

Reputation: 10695

Why does loop statement have uneven number of binding vectors?

I've been looking at examples and documentation; and I cannot figure out why I'm getting the

loop requires an even number of forms in binding vector error.

I believe the problem is in the loop and initializing usage-indicies. I just cannot figure out what I'm doing wrong.

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

Upvotes: 0

Views: 1070

Answers (1)

A. Webb
A. Webb

Reputation: 26466

A loop form should look just like a let form. You can add comma whitespace for clarity:

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

It looks like you might want

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

where curr-index is initialized (rebound) to curr-index from the let expression.

Better, you could move the initialization you having going on in your let form down to the loop form itself. It also looks as if usage-vec-len is just the count of usage-indices, so no need to specify that in a global def when you can take care of that in the loop binding as well.

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

Upvotes: 5

Related Questions