Debian Codegraph @

demo/kitchen-sink/docs/haskell.hs

raw
1 -- Type annotation (optional)
2 fib :: Int -> Integer
3
4 -- With self-referencing data
5 fib n = fibs !! n
6 where fibs = 0 : scanl (+) 1 fibs
7 -- 0,1,1,2,3,5,...
8
9 -- Same, coded directly
10 fib n = fibs !! n
11 where fibs = 0 : 1 : next fibs
12 next (a : t@(b:_)) = (a+b) : next t
13
14 -- Similar idea, using zipWith
15 fib n = fibs !! n
16 where fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
17
18 -- Using a generator function
19 fib n = fibs (0,1) !! n
20 where fibs (a,b) = a : fibs (b,a+b)