{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

The coroutines code, with notes

How to Design Programs: An Introduction to Programming and Computing

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
;; The first three lines of this file were inserted by DrScheme. ;; They record information about the language level. #reader(lib "plai-pretty-big-reader.ss" "plai")((modname coroutines) (read- case-sensitive #t) (teachpacks ())) ;; cs536: programming with continuations ;; The final coroutine code from class ;; Kathi Fisler, Oct 19, 2007 (define (make-coroutine from-proc) (let ([hold false]) (lambda () (if hold (hold 'dummy) (from-proc (lambda (resume-other) (let/cc k (set! hold k) (resume-other)))))))) (define corouteA (make-coroutine (lambda (switch) (begin (printf "a1~n") (switch corouteB) (printf "a2~n") (switch corouteB))))) (define corouteB (make-coroutine (lambda (switch) (begin (printf "b1~n") (switch corouteA) (printf "b2~n"))))) #| NOTES The key to "getting" this code is to actually work out the values of k (ie, the hold values). Do this very mechanically, following the rules of substitution. For example: (corouteA) = [by subst value of corouteA within parens] ((make-coroutine (lambda (switch) (begin (printf "a1~n") (switch corouteB) (printf "a2~n") (switch corouteB))))) = [by subst (lambda (switch) ...) for from-proc in make-coroutine] ((let ([hold false]) (lambda ()
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}