Create a gist now

Instantly share code, notes, and snippets.

Experiment with persistent continuations
#lang racket
;; Experiment with persistent continuations
(require db)
;; read-char* : like read-char but ignores \r and \n
(define (read-char* [in (current-input-port)])
(let ([v (read-char in)])
(case v
[(#\newline #\return) (read-char* in)]
[else v])))
;; A simple interactive counter.
;; Type + to increment, - do decrement, anything else to stop.
;; When stopped, the counter returns a continuation which
;; let's you continue using your counter from where you stopped.
(define (counter [init 0])
(displayln init)
(let/ec e
(+ init
(let/cc k
(case (read-char*)
[(#\+) 1]
[(#\-) -1]
[else (displayln "bye...") (e k)]))))))
(define sqlc (sqlite3-connect #:database 'memory))
(query-exec sqlc "create table counters (continuation)")
(query-exec sqlc "insert into counters values (?)"
(with-input-from-string "++++-+--." (thunk (counter))))
(query-list sqlc "select continuation from counters")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment