Skip to content

Instantly share code, notes, and snippets.

@alexserver
Created January 14, 2014 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexserver/8422050 to your computer and use it in GitHub Desktop.
Save alexserver/8422050 to your computer and use it in GitHub Desktop.
factorial in functional approach, with javascript
//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