# Fun with lambda and map

Andrew Gaul gaul at spam.utexas.edu
Tue Feb 12 22:08:26 CET 2002

```In article <slrna6i45q.sp.gaul at bifrost.cs.utexas.edu>, Andrew Gaul wrote:
> In article <slrna6i14c.sp.gaul at bifrost.cs.utexas.edu>, Andrew Gaul wrote:
>> In article <3c566a21.22129828 at news.t-online.de>, Gerson Kurz wrote:
>>> fibonacci = lambda x:map(lambda o:(map(lambda c:map(lambda l:o.__setslice__(l,l,l),([o+3,o+4,[o]],[0,3,[o,reduce(lambda x,o:x+o,o[:2]),o+1]])),range(x)),o),[[1,1,0]+range(x)])[3:]
>>
>>     fibonacci = lambda z: map(lambda n, f=(lambda f,x,a,b: \
>>     [x<=0 and b, x>0 and f(f,x-1,b,a+b)][x>0]): f(f,n,0,1), range(z))
>
>     fibonacci = lambda z, f=(lambda f,x,l: \
>     [x<=0 and l, x>0 and f(f,x-1,l+[l[-1]+l[-2]])][x>0]): f(f,z-2,[1,1])

In my zealousness to cut characters, I also sacrificied correctness for
the base case.  This should be proof enough that code like this should
never be written.  Here's a final, working version:

Python 2.2 (#5, Jan 23 2002, 16:23:03)
[GCC 3.0.3] on linux2
>>> fibonacci = lambda z, f=(lambda f,x,l=[0,1]: \
... [x<=0 and l, x>0 and f(f,x-1,l+[l[-1]+l[-2]])][x>0]): f(f,z-1)[1:]
>>> fibonacci(1)

>>> fibonacci(10)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

--
Andrew Gaul
http://gaul.org/

```