Skip to content

Instantly share code, notes, and snippets.

@tonyonodi
Last active September 12, 2015 16:27
Show Gist options
  • Save tonyonodi/8f979be19b968d522562 to your computer and use it in GitHub Desktop.
Save tonyonodi/8f979be19b968d522562 to your computer and use it in GitHub Desktop.
This is a translation of the code in Exercise 5.39 (p. 488) of SICP from Scheme into Javascript. Takes a while to wrap your head around. I think it's really really really pretty.
(function(n) {
return (function(fact_iter) {
return fact_iter(fact_iter, 1, 1)
})(function(f_i, product, counter) {
return counter > n ?
product :
f_i(f_i, counter * product, counter + 1)
})
})(5) // returns 5! or 120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment