Skip to content

Instantly share code, notes, and snippets.

@Yoxem
Last active July 27, 2018 20:27
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 Yoxem/bd4e5d304598d9c1441c63303055fa3d to your computer and use it in GitHub Desktop.
Save Yoxem/bd4e5d304598d9c1441c63303055fa3d to your computer and use it in GitHub Desktop.
A simple example of call/cc (call-with-current-continuation)
(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