Skip to content

Instantly share code, notes, and snippets.

@aoeuidht
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save aoeuidht/13d5f3fc0e3c9a9a5a19 to your computer and use it in GitHub Desktop.

Select an option

Save aoeuidht/13d5f3fc0e3c9a9a5a19 to your computer and use it in GitHub Desktop.
cps/trampoline version
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# the cps version
def s(n, f):
if n < 1:
return f(n)
return s(n-1,
lambda x: f(x + n))
# the trampoline version
def t_s(n, f):
if n < 1:
return lambda :f(n)
return lambda : t_s(n-1, lambda x: f(x + n))
def trampoline(f):
while hasattr(f, '__call__'):
f = f()
return f
if __name__ == '__main__':
print s(10, lambda x: x)
print trampoline(t_s(10, lambda x: x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment