Created
January 14, 2014 17:18
-
-
Save alexserver/8422050 to your computer and use it in GitHub Desktop.
factorial in functional approach, with javascript
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
//Here's the standard naive factorial: | |
function fact(n) { | |
if (n == 0) | |
return 1 ; | |
else | |
return n * fact(n-1) ; | |
} | |
//Here it is in CPS: | |
function fact(n,ret) { | |
if (n == 0) | |
ret(1) ; | |
else | |
fact(n-1, function (t0) { | |
ret(n * t0) }) ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment