Skip to content

Instantly share code, notes, and snippets.

@domgetter
Created October 23, 2015 20:15
Show Gist options
  • Save domgetter/916caea888a1126c8c4b to your computer and use it in GitHub Desktop.
Save domgetter/916caea888a1126c8c4b to your computer and use it in GitHub Desktop.
Recur recursive version of iterative functions
;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