-
-
Save aoeuidht/13d5f3fc0e3c9a9a5a19 to your computer and use it in GitHub Desktop.
cps/trampoline version
This file contains hidden or 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
| #!/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