Skip to content

Instantly share code, notes, and snippets.

@krishnabhargav
Created July 25, 2014 01:19
Show Gist options
  • Save krishnabhargav/10c39c276ea0f2c63cdf to your computer and use it in GitHub Desktop.
Save krishnabhargav/10c39c276ea0f2c63cdf to your computer and use it in GitHub Desktop.
two examples to help understand recur and loop....recur
(defn count-down [n]
(do
(println "Method Started")
(println "Number : " n)
(when (pos? n) (recur (dec n)))))
;; when you run (count-down 5), output is
;; Method Started
;; Number : 5
;; Method Started
;; Number : 4
;; ... so on ....
;;loop-recur
(defn count-down [n]
(do
(println "Method Started")
(loop [a n]
(println "Number : " a)
(when (pos? a) (recur (dec a))))))
;; (count-down 5000) will print method started only once.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment