Skip to content

Instantly share code, notes, and snippets.

@rhcarvalho
Created June 8, 2012 03:19
Show Gist options
  • Save rhcarvalho/2893350 to your computer and use it in GitHub Desktop.
Save rhcarvalho/2893350 to your computer and use it in GitHub Desktop.
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
(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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment