Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Weird continuation behavior (to me...)
#lang racket
;; Since the control-state blows itself away after the first call by
;; set!ing itself to a continuation, I would expect the return argument to
;; control-state to retain the value from it's first call. Therefore I
;; expected that calls subsequent to the first call to control state would
;; "return" execution back to the first call creating an infinite loop of
;; sorts, but in DrRacket 5.1 it outputs 0 1 and finishes execution.
;; [LISTOF X] -> ( -> X u 'you-fell-off-the-end)
(define (generate-one-element-at-a-time lst)
;; Hand the next item from a-list to "return" or an end-of-list marker
(define (control-state return)
(lambda (element)
(lambda (resume-here)
;; Grab the current continuation
(set! control-state resume-here)
(return element))))
(return 'you-fell-off-the-end))
;; (-> X u 'you-fell-off-the-end)
;; This is the actual generator, producing one item from a-list at a time
(lambda () (call-with-current-continuation control-state)))
(define generate-digit
(generate-one-element-at-a-time '(0 1 2 3 4 5 6 7 8 9)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment