Created
October 16, 2011 03:16
-
-
Save dsamarin/1290462 to your computer and use it in GitHub Desktop.
CPS Transforms in JavaScript and Scheme
This file contains hidden or 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 1 | |
function pyth(x, y) { | |
return Math.sqrt (x*x + y*y); | |
} | |
// Example 2 | |
function factorial(n) { | |
if (n === 0) { | |
return 1; | |
} | |
return n * factorial (n - 1); | |
} | |
// Example 3 | |
function factorial(n) { | |
return f_aux (n, 1); | |
} | |
function f_aux(n, a) { | |
if (n === 0) { | |
return a; | |
} | |
return f_aux (n - 1, n * a); | |
} |
This file contains hidden or 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 1 | |
function pyth(x, y, callback) { | |
mul (x, x, function(a) { | |
mul (y, y, function(b) { | |
add (a, b, function(c) { | |
sqrt (c, callback); | |
}) | |
}); | |
}); | |
} | |
// Example 2 | |
function factorial(n, callback) { | |
eq (n, 0, function(a) { | |
if (a) { | |
callback (1); | |
} else { | |
sub (n, 1, function(b) { | |
factorial (b, function(c) { | |
mul (n, c, callback); | |
}); | |
}); | |
} | |
}); | |
} | |
// Example 3 | |
function factorial(n, callback) { | |
f_aux (n, 1, callback); | |
} | |
function f_aux(n, m, callback) { | |
eq (n, 0, function(a) { | |
if (a) { | |
callback (m); | |
} else { | |
sub (n, 1, function(b) { | |
mul (n, m, function(c) { | |
f_aux (b, c, callback); | |
}); | |
}); | |
} | |
}); | |
} | |
// Shared | |
function eq(a, b, callback) { | |
callback (a === b); | |
} | |
function mul(a, b, callback) { | |
callback (a * b); | |
} | |
function add(a, b, callback) { | |
callback (a + b); | |
} | |
function sub(a, b, callback) { | |
callback (a - b); | |
} | |
function sqrt(n, callback) { | |
callback (Math.sqrt (n)); | |
} |
This file contains hidden or 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 1 | |
(define (pyth x y) | |
(sqrt (+ (* x x) (* y y)))) | |
; Example 2 | |
(define (factorial n) | |
(if (= n 0) | |
1 ; NOT tail-recursive | |
(* n (factorial (- n 1)))) | |
; Example 3 | |
(define (factorial n) | |
(f-aux n 1)) | |
(define (f-aux n a) | |
(if (= n 0) | |
a ; tail-recursive | |
(f-aux (- n 1) (* n a)))) |
This file contains hidden or 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 1 | |
(define (pyth& x y k) | |
(*& x x (lambda (x2) | |
(*& y y (lambda (y2) | |
(+& x2 y2 (lambda (x2py2) | |
(sqrt& x2py2 k)))))))) | |
; Example 2 | |
(define (factorial& n k) | |
(=& n 0 (lambda (b) | |
(if b ; growing continuation | |
(k 1) ; in the recursive call | |
(-& n 1 (lambda (nm1) | |
(factorial& nm1 (lambda (f) | |
(*& n f k))))))))) | |
; Example 3 | |
(define (factorial& n k) (f-aux& n 1 k)) | |
(define (f-aux& n a k) | |
(=& n 0 (lambda (b) | |
(if b ; same continuation | |
(k a) ; in the recursive call | |
(-& n 1 (lambda (nm1) | |
(*& n a (lambda (nta) | |
(f-aux& nm1 nta k))))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment