2009-12-01 12 views
1

Est-il possible d'ajouter une autre fonction procC ici pour que la séquence d'évaluation soit procA-> procB-> procC-> procA ...?Puis-je avoir une co-routine de trois fonctions utilisant des continuations dans Scheme?

(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))))) 

Répondre

4

De "Le langage de programmation Scheme"

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! 
0

Quelque chose comme ça?

(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)