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
;; delay, force | |
;; Control Operations http://www.scheme.com/tspl4/control.html#./control:h7 | |
;; (define delay (with-module gauche delay)) | |
;; (define force (with-module gauche force)) | |
(define-syntax let/ | |
(syntax-rules () | |
((_ val (var ...) body ...) | |
(let ((v val)) | |
(let ((var v) ...) | |
body ...))))) | |
(define-syntax my-delay | |
(syntax-rules () | |
((_ expr) | |
(make-promise (lambda () | |
expr))))) | |
(define (make-promise p) | |
(let/ #f (val set?) | |
(lambda () | |
(unless set? | |
(let ((x (p))) | |
(unless set? | |
(set! val x) | |
(set! set? #t)))) | |
val))) | |
(define (force promise) | |
(promise)) | |
(define (stream-car s) | |
(car (my-force s))) | |
(define (stream-cdr s) | |
(cdr (my-force s))) | |
(define counters | |
(let next ((n 1)) | |
(my-delay (cons n (next (+ n 1)))))) | |
(stream-car counters) | |
; -> 1 | |
(stream-car (stream-cdr counters)) | |
; -> 2 | |
(define (stream-add s1 s2) | |
(my-delay (cons | |
(+ (stream-car s1) | |
(stream-car s2)) | |
(stream-add (stream-cdr s1) | |
(stream-cdr s2))))) | |
(define even-counters | |
(stream-add counters counters)) | |
(stream-car even-counters) | |
; -> 2 | |
(stream-car (stream-cdr even-counters)) | |
; -> 4 |
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
;; values | |
;; 多値を学ぶ - ひげぽん OSとか作っちゃうかMona- http://d.hatena.ne.jp/higepon/20080608/1212937386 | |
(define (values . things) | |
(call-with-current-continuation | |
(lambda (cont) (apply cont things)))) |