Created
July 25, 2014 01:19
-
-
Save krishnabhargav/10c39c276ea0f2c63cdf to your computer and use it in GitHub Desktop.
two examples to help understand recur and loop....recur
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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