Created
October 20, 2017 03:34
-
-
Save xuanruiqi/5e77e1a1cc786b98e8d2b15758f1e580 to your computer and use it in GitHub Desktop.
Continuation-passing style example
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
| (define (fact-cps* n k) | |
| (if (= n 0) | |
| (k 1) ;; "throw" 1 into the continuation and we're done | |
| ;; CPS is a bit like accumulator style | |
| (fact-cps* (- n 1) ;; recurse with (- n 1) | |
| (lambda (v) (k (* n v)))))) ;; throw result into continuation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment