Last active
February 1, 2019 13:01
-
-
Save haruo-wakakusa/3b69c7f884c6507cd429abe59d0b962c to your computer and use it in GitHub Desktop.
コルーチンのサンプル
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 | |
; | |
; 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