0

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

        )
    )

 )
)
3
  • Regarding the comment you have on 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 of loop? Commented Sep 7, 2014 at 23:56
  • That was a previous version of the code. I erased the comment for it. There is now an example of how adjustedCandyAmounts meets the conditions but still i am getting infinite loops. It must the the data structure. Commented Sep 8, 2014 at 1:09
  • ([current]) should be [current] - the extra parens will cause an exception. Commented Sep 8, 2014 at 1:47

1 Answer 1

2

Your termination condition is odd. (= (nth current 0) (nth current 1) (nth current 2) 2) means that you are testing whether the first three elements of current all equal 2.

In myLoopFunc you are iterating on adjustedCandyAmounts until the output is [2 2 2 n] (you don't test the fourth value). If adjustedCandyAmounts is called with [4 4 4 n] as an argument, it will return [4 4 4 n+1]. There is no way for the code to hit your termination condition with this input.

The general idea in recursion is that there must be some argument that is guaranteed to be getting closer to an end condition. For example you could have a number that decrements on each cycle and you stop when it hits 0, or you could have a sequence that grows shorter and you stop when it is empty. This code runs indefinitely because there is no way to reach the termination condition.

Finally, some points about style:

Please use standard paren placement, comment placement, whitespace, and indentation. The formatting you have now is hard to read.

In context, (into [] (flatten (vector a b c d))) is just an inefficient and hard to read way of writing [a b c d].

(conj [] current) is always better written as [current].

Sign up to request clarification or add additional context in comments.

2 Comments

the if condition is definitely wrong i c that now. I will also make the adjustment for the vector.
Is (nth current 0) (nth current 1) (nth current 2) updating values per each iteration or does it remain constant? I need it to change per every iteration. Also I have to remove the 2 from the if condition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.