public
Created

Weird continuation behavior (to me...)

  • Download Gist
gistfile1.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 33
#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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.