Created
August 7, 2023 02:17
-
-
Save nikhilm/62c5bd78e138f89764bdc4baa05fa908 to your computer and use it in GitHub Desktop.
experimenting with continuations in racket
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
#lang racket/base | |
(define kont #f) | |
(define my-prompt (make-continuation-prompt-tag)) | |
(begin | |
(call-with-continuation-prompt | |
(λ () | |
(printf "Before everything~n") | |
(printf "The call current/composable returned ~v~n" | |
(call/cc | |
(lambda (k) | |
(printf "Proc started running~n") | |
(set! kont k) | |
(k k) | |
(printf "End of proc~n")) | |
my-prompt)) | |
(printf "After the call itself~n")) | |
my-prompt) | |
(printf "Outside the prompt~n")) | |
(call-with-continuation-prompt (lambda () (kont 5)) my-prompt) | |
(printf "--------------------------~n") | |
(begin | |
(call-with-continuation-prompt | |
(λ () | |
(printf "Before everything~n") | |
(printf "The call current/composable returned ~v~n" | |
(call-with-composable-continuation | |
(lambda (k) | |
(printf "Proc started running~n") | |
(set! kont k) | |
(k k) | |
(printf "End of proc~n")) | |
my-prompt)) | |
(printf "After the call itself~n")) | |
my-prompt) | |
(printf "Outside the prompt~n")) | |
(call-with-continuation-prompt (lambda () (kont 5)) my-prompt) | |
(printf "--------------------------~n") | |
(define exit-deep (lambda () (abort-current-continuation my-prompt))) | |
(define (deep) | |
(printf "start deep~n") | |
(deeper) | |
(printf "end deep~n")) | |
(define (deeper) | |
(printf "start deeper~n") | |
(exit-deep) | |
(printf "end deeper~n")) | |
(call-with-continuation-prompt | |
deep | |
my-prompt | |
(lambda () (void))) | |
(printf "--------------------------~n") | |
;; using the default handler | |
(define (raise exn proc) | |
(abort-current-continuation | |
my-prompt | |
(lambda () | |
(when (equal? exn 'the-one-we-are-looking-for) | |
(proc))))) | |
(define (deep2) | |
(printf "start deep2~n") | |
(deeper2) | |
(printf "end deep2~n")) | |
(define (deeper2) | |
(printf "start deeper2~n") | |
; replace 'foo with the other value to see this print | |
(raise 'foo (lambda () (printf "default handler calls this~n"))) | |
(printf "end deeper2~n")) | |
(call-with-continuation-prompt | |
deep2 | |
my-prompt) | |
;; understanding call-in-continuation | |
(printf "--------------------------~n") | |
(printf "This evaluates to ~v~n" | |
(+ 1 | |
(call/cc (lambda (k) (k 4))))) | |
(printf "This evaluates to ~v~n" | |
(+ 1 | |
(call/cc (lambda (k) | |
(call-in-continuation k (lambda () 4)))))) | |
;; yea not sure what is happening. see the example in the docs about how dynamic-wind's value is available or something | |
;; playing with let/cc | |
(printf "--------------------------~n") | |
(begin | |
(call-with-continuation-prompt | |
(λ () | |
(printf "Before everything~n") | |
(printf "The call current/composable returned ~v~n" | |
(let/cc k | |
(printf "Proc started running~n") | |
(set! kont k) | |
(k k) | |
(printf "End of proc~n"))) | |
(printf "After the call itself~n"))) | |
(printf "Outside the prompt~n")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment