Last active
September 12, 2015 16:27
-
-
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.
This file contains 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(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