Created
October 23, 2015 20:15
-
-
Save domgetter/916caea888a1126c8c4b to your computer and use it in GitHub Desktop.
Recur recursive version of iterative functions
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
;function fib(n) { | |
; var next, acc = 1, 0; | |
; while(n > 0) { | |
; next, acc = next + acc, next; | |
; n--; | |
; } | |
; return acc; | |
;} | |
(defn fib [n next acc] | |
(if (= 0 n) | |
acc | |
(recur (dec n) (+ next acc) next))) | |
;function fact(n) { | |
; var acc = 1; | |
; while(n > 1) { | |
; acc = acc * n; | |
; n--; | |
; } | |
;} | |
(defn fact [n acc] | |
(if (= 1 n) | |
acc | |
(recur (dec n) (* n acc)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment