You're viewing version 1.2.0 of recur. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

recur

clojure.core

4 Examples top

  • (def factorial
      (fn [n]
        (loop [cnt n acc 1]
           (if (zero? cnt)
                acc
              (recur (dec cnt) (* acc cnt))
    ; in loop cnt will take the value (dec cnt)
    ; and acc will take the value (* 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))))
  • (loop [i 0]  
      (when (< i 5)    
        (println i)    
        (recur (inc i)); loop i will take this value
    ))
  • (defn compute-across [func elements value]
      (if (empty? elements)
        value
        (recur func (rest elements) (func value (first elements)))))
    
    (defn total-of [numbers]
      (compute-across + numbers 0))
    
    (defn larger-of [x y]
      (if (> x y) x y))
    
    (defn greatest-of [numbers]
      (compute-across larger-of numbers (first numbers)))
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.