Created
August 6, 2011 09:06
-
-
Save halter73/1129202 to your computer and use it in GitHub Desktop.
Weird continuation behavior (to me...)
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 | |
;; 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) | |
(for-each | |
(lambda (element) | |
(call-with-current-continuation | |
(lambda (resume-here) | |
;; Grab the current continuation | |
(set! control-state resume-here) | |
(return element)))) | |
lst) | |
(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))) | |
(generate-digit) | |
(generate-digit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment