Skip to content Skip to sidebar Skip to footer

Python: Nested Lambdas -- `s_push: Parser Stack Overflow Memory Error`

I recently stumbled across this article which describes how to code FizzBuzz using only Procs in Ruby, and since I was bored, thought it would be neat to try and implement the same

Solution 1:

express the number 100 without having so many nested functions?

here you go:

>>>test = lambda f: f(lambda x: x + 1)(0)>>>z = lambda f: lambda x: x>>>test(z)
0
>>>succ = lambda n: lambda f: lambda x: f(n(f)(x))>>>_1 = succ(z)>>>test(_1)
1
>>>_2 = succ(_1)>>>test(_2)
2
>>>plus = lambda m: lambda n: lambda f: lambda x: m(f)(n(f)(x))>>>_3 = plus(_1)(_2)>>>test(_3)
3
>>>mult = lambda m: lambda n: lambda f: lambda x: m(n(f))(x)>>>_6 = mult(_2)(_3)>>>test(_6)
6
>>>_5 = plus(_2)(_3)>>>_25 = mult(_5)(_5)>>>_4 = plus(_2)(_2)>>>_100 = mult(_25)(_4)>>>test(_100)
100
>>>

Solution 2:

It looks like it's not possible without recompiling Python. The parser stack size is set with the constant MAXSTACK in parser.h. You could increase this value and recompile to increase the limit. See http://bugs.python.org/issue3971 and http://mail.python.org/pipermail/python-list/2012-March/621555.html .

Solution 3:

From the lambda-calculus standpoint, incrementing a number can be done with the following function:

succ = lambda n: lambda p: lambda x: p(n(p)(x))

Then, one = succ(zero), two = succ(one), and so forth.

Post a Comment for "Python: Nested Lambdas -- `s_push: Parser Stack Overflow Memory Error`"