The reason why i
is decreasing with each call is that i
is used to determine when we've reached the end of the vector. The idea is that when we call the helper
function initially, we set i
to be the length of the vector (minus 2), and then in each recursive call decrease i
by 1 until it becomes less than 0, at which point we know that we've gone through the whole vector.
The issue causing your confusion, I believe, is that you're parsing the cons
call incorrectly- counting the parentheses shows us that the call is really (cons (vector-ref v i) r)
- the (- i 1)
is just the third argument to helper
.
Finally, the idea of checking in line 9 that the length of the vector is greater than 0 is that if the vector's length is 0, we just want to return an empty list '()
; if we didn't do this check, then the call to (vector-ref v (- (vector-length v) 1))
would become (vector-ref v -1)
if we input an empty vector, which would obviously fail. I'm not sure what you mean by "total length checking", but (vector-length v)
does indeed return the entire length of v
.