Skip to content

Instantly share code, notes, and snippets.

@suriyadeepan
Created May 3, 2019 07:55
Show Gist options
  • Save suriyadeepan/9e591c51aca49764f8d930a460378c7c to your computer and use it in GitHub Desktop.
Save suriyadeepan/9e591c51aca49764f8d930a460378c7c to your computer and use it in GitHub Desktop.
CPS in Python
def factorial(n):
if n == 0:
return 1
return factorial(n - 1) * n
def factorial_cps(k, n):
if n == 0:
k(1)
else:
factorial_cps(
lambda x : k(n * x),
n - 1
)
if __name__ == '__main__':
factorial_cps(print, 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment