Skip to content

Instantly share code, notes, and snippets.

@cametan001
Created November 29, 2012 19:48
Show Gist options
  • Save cametan001/4171396 to your computer and use it in GitHub Desktop.
Save cametan001/4171396 to your computer and use it in GitHub Desktop.
Pythonで継続渡し(CPS)
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))))
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment