recur

clojure.core

2 Examples top

  • (def factorial
      (fn [n]
        (loop [cnt n acc 1]
           (if (zero? cnt)
                acc
              (recur (dec cnt) (* acc cnt))))))
  • ; A loop that sums the numbers 10 + 9 + 8 + ...
    
    ; Set initial values count (cnt) from 10 and down
    (loop [sum 0 cnt 10]
        ; If count reaches 0 then exit the loop and return sum
        (if (= cnt 0)
        sum
        ; Otherwise add count to sum, decrease count and 
        ; use recur to feed the new values back into the loop
        (recur (+ cnt sum) (dec cnt))))
Log in to add / edit an example.

See Also top

Log in to add a see also.

Comments top

No comments for recur. Log in to add a comment.