我正在查看来自http://community.schemewiki.org/?call-with-current-continuation的协程的以下示例:
(define (hefty-computation do-other-stuff)
(let loop ((n 5))
(display "Hefty computation: ")
(display n)
(newline)
(set! do-other-stuff (call/cc do-other-stuff)) ; point A
(display "Hefty computation (b)")
(newline)
(set! do-other-stuff (call/cc do-other-stuff))
(display "Hefty computation (c)")
(newline)
(set! do-other-stuff (call/cc do-other-stuff))
(if (> n 0)
(loop (- n 1)))))
多余的工作:
;; notionally displays a clock
(define (superfluous-computation do-other-stuff)
(let loop ()
(for-each (lambda (graphic)
(display graphic)
(newline)
(set! do-other-stuff (call/cc do-other-stuff)))
'("Straight up." "Quarter after." "Half past." "Quarter til.")) ; point B
(loop)))
(hefty-computation superfluous-computation)
对于 call/cc 的第一次使用,上下文应该是什么?当我说上下文时,我的意思是由于 callcc 的跳转,我们应该“返回”到哪里?
据我了解,第一次调用 call/cc 时,do-other-stuff 本质上变成了一个过程,执行多余计算的代码,然后在集合之后立即跳转到该点!(A点)。第二次,它将“跳转到 B 点”行为包裹在“跳转到 A 点并执行上下文,或 A 点之后的任何代码”周围。它是否正确?
如果设置,此代码似乎不起作用!实际发生了。或者是套装!这段代码有必要工作吗?
对正在发生的事情进行可视化表示确实会有所帮助。