- なんでも継続からleaf-count/cpsThis file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
def leaf_count(tree, cont): if isinstance(tree, list): if tree == []: cont(0) else: leaf_count(tree[0],\ lambda n :\ leaf_count(tree[1:],\ lambda m : cont(m + n))) else: cont(1) - SchemeとActor理論から_factThis file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
def add(x, y, k): k(x + y) def subtract(x, y, k): k(x - y) def multiply(x, y, k): k(x * y) def isequivalent(x, y, k): k(x == y) def fact(n, k): isequivalent(n, 0, \ lambda u : \ k(1) \ if u \ else subtract(n, 1, \ lambda v : fact(v, \ lambda w : multiply(n, w, k))))
2012年11月30日金曜日
Pythonで継続渡し(CPS)
登録:
コメントの投稿 (Atom)
とても久々な更新は結局Lispなんですね(ぇ
返信削除これでPythonでScheme書いても継続の機能が比較的簡単に実装出来る事が分かったわけですしおすし。
返信削除