I have updated the code below with noisesmith comments (except for the format I dont know how it should be...
The idea is that given a vector [2 8 4 0] that it will eventually return [8 8 8 n] where n is the amount of recursion it takes to get to that result. Where first 3 inputs are even. The first 3 inputs could be any even natural number.
Let me provide an example:
(adjustedCandyAmounts [12 4 24 1]) [18 8 18 2] So know I iterate (recur... i guess) (adjustedCandyAmounts [18 8 18 3]) [18 14 18 4] recur... (adjustedCandyAmounts [18 14 18 3]) [18 16 18 4] Finally we reach our condition... (adjustedCandyAmounts [18 16 18 4]) [18 18 18 5] At this point the if statement should kick in.. So techniquely i just want to see a different paramater (vector) to adjustedCandyAmounts until the condition is met.
Is current not being updated with the latest vector that represents current?
If I use the repl to call (adjustedCandyAmounts [4 2 4 0]) adjustedCandyAmounts Returns: [4 4 4 2]
My expectation is that the myLoopFunc (function) with a vector called current array will "update" with the "new" (i know clojure doesn't do a new vector...) vector received and evaluate the condition again. Then why is it that if I try
(myLoopFunc [4 2 4 0]) it goes to infinity and beyond.
(
defn calcAdjusted [[candyOwned adjacentCandy]]
(let [TotalCandy (+
(quot candyOwned 2) ; i had to give half of my candies to someone else
(quot adjacentCandy 2); i receive half of someone elses candy
)]
(if (odd? TotalCandy)
(+ TotalCandy 1);"Yes is an odd"
TotalCandy ;"is not an odd number"
)
)
)
(defn adjustedCandyAmounts [[r s t u]]
(let
[
rUpdated (calcAdjusted [r t]); r receives half of what t owns
sUpdated (calcAdjusted [s r]); s receives half of what r owns
tUpdated (calcAdjusted [t r]); t receives half of what s owns
counterIncremented (inc u)
]
; (println rUpdated)
(vector rUpdated sUpdated tUpdated counterIncremented )
)
)
(defn myLoopFunc [[r s t u]]
(
let [candyInitial [r s t u]]
(
loop [ current candyInitial]
(
if (= (nth current 0) (nth current 1) (nth current 2) )
[current]
(recur (adjustedCandyAmounts current)) ;
)
)
)
)
recur- what second arg are you talking about? There is nothing in your code that takes more than one arg. Are you misunderstanding the syntax ofloop?