2 ms·
> f=lambda n:(b:=2<<n)*n*b//(b*b-b-1)%b > [f(n) for n in range(10)] > [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] Or in Haskell, without the cute generating function t
by tromp 3y ago
> f=lambda n:(b:=2<<n)*n*b//(b*b-b-1)%b
> [f(n) for n in range(10)]
> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Or in Haskell, without the cute generating function tricks:
take 10 $ let f a b = a : f b (a+b) in f 0 1
- nh23423fefe 3y agof = 0:1:zipWith (+) f (tail f)
- Qem 3y agoIn Python is possible to do something quite similar, using itertools: from itertools import pairwise, chain def f(): yield from chain([0,1], map(sum, pairwise(f()))) There's a Joel Grus post discussing it: https://joelgrus.com/2015/07/07/haskell-style-fibonacci-in-python/ https://joelgrus.com/2015/07/07/haskell-style-fibonacci-in-p... The introduction of pairwise in 3.10 made possible to write it more compactly, when compared to the version discussed by Joel.
- lapinot 3y agoThis article is about the cute generating function trick. Not exactly sure why you're showcasing the usual recursive fibonacci in haskell.