I recently came across this article that describes how to encode FizzBuzz using only Procs in Ruby, and since I was bored, I thought it would be neat to try to implement the same thing in Python using lambdas.
I ended up in the section where you create numbers using nested functions and wrote the following Python script:
#!/usr/bin/env python zero = lambda p : (lambda x: x) one = lambda p : (lambda x: p(x)) two = lambda p : (lambda x: p(p(x))) three = lambda p : (lambda x: p(p(p(x)))) five = lambda p: (lambda x: p(p(p(p(p(x)))))) fifteen = lambda p : (lambda x: p(p(p(p(p( \ p(p(p(p(p( \ p(p(p(p(p(x)))))))))))))))) hundred = lambda p: (lambda x: p(p(p(p(p(p(p(p(p(p( \ p(p(p(p(p(p(p(p(p(p( \ p(p(p(p(p(p(p(p(p(p( \ p(p(p(p(p(p(p(p(p(p( \ p(p(p(p(p(p(p(p(p(p( \ p(p(p(p(p(p(p(p(p(p( \ p(p(p(p(p(p(p(p(p(p( \ p(p(p(p(p(p(p(p(p(p( \ p(p(p(p(p(p(p(p(p(p( \ p(p(p(p(p(p(p(p(p(p(x)))))))))))))))))))))))))))) \ ))))))))))))))))))))))))))) \ ))))))))))))))))))))))))))) \ ))))))))))))))))))) def to_int(func): return func(lambda n: n + 1)(0) print to_int(zero) print to_int(one) print to_int(two) print to_int(three) print to_int(five) print to_int(fifteen) print to_int(hundred)
A number from zero to fifteen works fine, but if I try to create the number 100, the file will not be launched due to the following error:
s_push: parser stack overflow
Memoryerror
I need to comment on this so that the file generally runs.
So sucks - is there any way to limit this so that I can arbitrarily insert lambdas and function calls without losing Python and running out of memory?
Or, alternatively, is there any trick of lambda calculus that I can use to express the number 100 without having a lot of nested functions?
source share