English 中文(简体)
Can 我有三种功能的共同路线,使用继续留在计划中?
原标题:Can I have a co-routine of three functions using continuations in Scheme?

是否有可能在此增加另一项职能,以便评价的顺序为:procA->procC->procC->procA......?

(define (procA another-fun)
  (let loop ((n 5))
    (display "In Proc A 
")
    (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  
")
    (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)




相关问题
Parsing with DCGs in Scheme (without Prolog)?

Lots of Prolog-in-Scheme implementations are out there. E.g. Kanren, Schelog. Apparently in "Paradigms of AI Programming" Norvig implements Prolog-to-Lisp compiler in Lisp in order to use Definite ...

Applying a symbol as a procedure

Suppose I have a simple symbol: > + + Is there any way I can apply that symbol as a procedure: > ((do-something-with +) 1 2) 3 So that + is evaluated to the procedure +?

string-split in DrScheme

How do I do equivalent of python s str.split in DrScheme? SRFI-13 doesn t seem to have it provided.

Scheme, getting the pointer from pointed struct

Assume I have a such struct: (define-struct node (value next)) ;and making 2 nodes, parent pointing to child as next. (define child (make-node 2 null)) (define parent (make-node 1 child)) Under ...

How to solve the following equation using accumulate (Scheme)

I m trying to do the following problem (there is a formula so I print-screened and uploaded it) Formula http://img248.imageshack.us/img248/6558/problemh.jpg (http://img248.imageshack.us/img248/6558/...

热门标签