1

Pythonコードで書かれたスキーム手順を理解しようとしています:

def callcc(proc):
    "Call proc with current continuation; escape only"
    ball = RuntimeWarning("Sorry, can't continue this continuation any longer.")
    def throw(retval): ball.retval = retval; raise ball
    try:
        return proc(throw)
    except RuntimeWarning as w:
        if w is ball: return ball.retval
        else: raise w

このチュートリアルからのものです: http://norvig.com/lispy2.html .

上記はどのように機能しますか?どういうball意味ですか? また、なぜ a proc(edure?) がthrow引数値として a で呼び出されるのでしょうか? そして、「エスケープのみ」というコメントはどういう意味ですか?


ちなみに、Pythonに適用される継続についての私の現在の(おそらく見当違いの)理解は次のとおりです。これは、yieldで関数を渡すことに似ています。

def c(func, *args, **kwargs):
    # func must be a coroutine
    return func(*args, **kwargs)

def inc(x=0):
    while True:
        yield x
        x += 1

>>> ct=c(inc, 3)
>>> next(ct)
3
>>> next(ct)
4
4

3 に答える 3