public

Experiment with persistent continuations

  • Download Gist
counters-with-countinuations.rkt
Racket
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#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
(counter
(+ 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")

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.