Skip to content

Instantly share code, notes, and snippets.

@xuanruiqi
Created October 20, 2017 03:34
Show Gist options
  • Select an option

  • Save xuanruiqi/5e77e1a1cc786b98e8d2b15758f1e580 to your computer and use it in GitHub Desktop.

Select an option

Save xuanruiqi/5e77e1a1cc786b98e8d2b15758f1e580 to your computer and use it in GitHub Desktop.
Continuation-passing style example
(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