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.

;; The first three lines of this file were inserted by DrScheme. ;; They record information about the language level. #reader(lib "" "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.

Image of page 2
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '07
  • Fisler
  • printf, Coroutine, lambda, Kathi Fisler

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern