Created
June 8, 2012 03:19
-
-
Save rhcarvalho/2893350 to your computer and use it in GitHub Desktop.
Experiment with persistent continuations
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 | |
;; 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