Last active
July 27, 2018 20:27
-
-
Save Yoxem/bd4e5d304598d9c1441c63303055fa3d to your computer and use it in GitHub Desktop.
A simple example of call/cc (call-with-current-continuation)
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
(define (g x) | |
(display "萬歲") | |
(cos (x 18))(x 20) | |
(display "ura") | |
72 | |
) | |
(call-with-current-continuation g) | |
;; prints 萬歲; then returns 18 | |
(g cos) | |
(define | |
(h x) | |
(display "萬歲") | |
(cos (x 18)) | |
(x 20) | |
(display "ura") | |
72 | |
(+ 2 2) | |
) | |
(h cos) | |
;;prints 萬歲ura; returns 4 4 | |
(define (j x) | |
(display "萬歲")(cos (x 18))(x 20) | |
(display "ura") | |
72 | |
(call-with-current-continuation j)) | |
(j cos) | |
;; prints 萬歲ura萬歲; returns 18 | |
(define c #f) | |
(define k #f) | |
(define a 5) | |
(define n 0) | |
(call-with-current-continuation (lambda (kk)( | |
(let foo () | |
(if (= a 5) | |
(begin (+ 7 | |
(/ 5 (call-with-current-continuation | |
(lambda (cc) | |
(set! c cc) | |
(set! k kk) | |
(k 6))))) | |
(display "cat\n") | |
(display n) | |
(set! n (+ n 1)) | |
(set! a 3) | |
(foo)) | |
(begin (display "123\n")(foo)))) | |
))) | |
; -> returns 6 | |
(c 12) | |
; -> prints | |
;cat | |
;0123 | |
;123 | |
;123 | |
;... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment