체계에서 연속성을 사용하여 세 가지 기능의 공동 경로를 가질 수 있습니까?
-
11-09-2019 - |
문제
평가 순서가 proca-> procb-> procc-> proca ...?
(define (procA another-fun)
(let loop ((n 5))
(display "In Proc A \n")
(set! another-fun (call/cc another-fun))
(when (> n 0)
(loop (- n 1)))))
(define (procB another-fun)
(let loop ((n 5))
(display "In Proc B \n")
(set! another-fun (call/cc another-fun))
(when (> n 0)
(loop (- n 1)))))
해결책
"체계 프로그래밍 언어"에서
http://www.scheme.com/tspl4/further.html#./further:h3
(define lwp-list '()) ; SO's highlighter gets confused
(define lwp
(lambda (thunk)
(set! lwp-list (append lwp-list (list thunk)))))
(define start
(lambda ()
(let ([p (car lwp-list)])
(set! lwp-list (cdr lwp-list))
(p))))
(define pause
(lambda ()
(call/cc
(lambda (k)
(lwp (lambda () (k #f)))
(start)))))
(lwp (lambda () (let f () (pause) (display "h") (f))))
(lwp (lambda () (let f () (pause) (display "e") (f))))
(lwp (lambda () (let f () (pause) (display "y") (f))))
(lwp (lambda () (let f () (pause) (display "!") (f))))
(lwp (lambda () (let f () (pause) (newline) (f))))
(start) hey!
hey!
hey!
hey!
다른 팁
이 같은?
(define (puts . lst)
(map display lst)
(newline))
(define (A some-fun more-fun)
(let loop ((n 3))
(puts "In A with " some-fun " and " more-fun " and n=" n)
(let-values (((s m) (call/cc (lambda (next-iter) (some-fun more-fun next-iter)))))
(set! some-fun s)
(set! more-fun m))
(when (> n 0) (loop (- n 1)))))
(define (B some-fun more-fun)
(let loop ((n 3))
(puts "In B with " some-fun " and " more-fun " and n=" n)
(let-values (((s m) (call/cc (lambda (next-iter) (some-fun more-fun next-iter)))))
(set! some-fun s)
(set! more-fun m))
(when (> n 0) (loop (- n 1)))))
(define (C some-fun more-fun)
(let loop ((n 3))
(puts "In C with " some-fun " and " more-fun " and n=" n)
(let-values (((s m) (call/cc (lambda (next-iter) (some-fun more-fun next-iter)))))
(set! some-fun s)
(set! more-fun m))
(when (> n 0) (loop (- n 1)))))
(A B C)
제휴하지 않습니다 StackOverflow