Created
May 24, 2019 09:48
-
-
Save elanpang/90054af4f3a08d0de3446ae193375303 to your computer and use it in GitHub Desktop.
// source https://jsbin.com
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// Example: Naive factorial | |
// 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); | |
}) ; | |
} | |
// And, to "use" the function, we pass it a callback: | |
fact (5, function (n) { | |
console.log(n) ; // Prints 120 in Firebug. | |
}) | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// Example: Naive factorial | |
// 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); | |
}) ; | |
} | |
// And, to "use" the function, we pass it a callback: | |
fact (5, function (n) { | |
console.log(n) ; // Prints 120 in Firebug. | |
})</script></body> | |
</html> |
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
// Example: Naive factorial | |
// 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); | |
}) ; | |
} | |
// And, to "use" the function, we pass it a callback: | |
fact (5, function (n) { | |
console.log(n) ; // Prints 120 in Firebug. | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment