Skip to content

Instantly share code, notes, and snippets.

@haruo-wakakusa
Last active February 1, 2019 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haruo-wakakusa/3b69c7f884c6507cd429abe59d0b962c to your computer and use it in GitHub Desktop.
Save haruo-wakakusa/3b69c7f884c6507cd429abe59d0b962c to your computer and use it in GitHub Desktop.
コルーチンのサンプル
#lang racket
;
; original code ->
; http://www.shido.info/lisp/scheme_cc.html
;
(require data/queue)
; coroutine
(define process-queue (make-queue))
(define (coroutine thunk)
(enqueue! process-queue thunk))
(define (start)
((dequeue! process-queue)))
(define (pause)
(let/cc k
(coroutine (lambda () (k)))
(start)))
; example
(coroutine (lambda ()
(let loop ((i 0))
(when (< i 10)
(display (+ 1 i))
(display " ")
(pause)
(loop (+ 1 i))))))
(coroutine (lambda ()
(let loop ((i 0))
(when (< i 10)
(display (integer->char (+ i 97)))
(display " ")
(pause)
(loop (+ 1 i))))))
(start)
(newline)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment