Skip to content

Instantly share code, notes, and snippets.

@elanpang
Created May 24, 2019 09:48
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 elanpang/90054af4f3a08d0de3446ae193375303 to your computer and use it in GitHub Desktop.
Save elanpang/90054af4f3a08d0de3446ae193375303 to your computer and use it in GitHub Desktop.
// source https://jsbin.com
<!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>
// 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