Skip to content

Instantly share code, notes, and snippets.

@prestonp
Created April 19, 2015 03:49
Show Gist options
  • Save prestonp/200bf4e0d13b0676088a to your computer and use it in GitHub Desktop.
Save prestonp/200bf4e0d13b0676088a to your computer and use it in GitHub Desktop.
mal tco factorial
// not tail call optimized
(def! fact (fn* (x) (if (= x 0) 1 (* x (fact (- x 1) )) )))

// tail call opt
(def! fact-tail (fn* [x accum] (if (= x 0) accum (fact-tail (- x 1) (* x accum)))))
(def! fact (fn* [x] (fact-tail x 1)) )

No formatting because... newlines mess up interpreter parsing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment