1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
module AufgabeFFP1
where
-------------------------------------------------------------------------------
-- Helpers
-- !! Index function for Integer-type
get :: [a] -> Integer -> a
get (x:xs) 0 = x
get (x:xs) n = get xs (n-1)
-------------------------------------------------------------------------------
-- recursive version
pof2s :: [Integer]
pof2s = 1:(map (2*) pof2s)
-- alternate:
-- calculates 2^ with each element of the continuous list
-- pof2s :: [Integer]
-- pof2s = map (2^) [0..]
-- alternate 2:
-- pof2s :: [Integer]
-- pof2s = iterate (2*) 1
-------------------------------------------------------------------------------
-- recursive version:
pd :: [[Integer]]
pd = [1] : (zipWith (\x y -> zipWith (+) ([0]++x) (y++[0])) pd pd)
-- alternate:
-- pd :: [[Integer]]
-- pd = iterate (\row -> zipWith (+) ([0] ++ row) (row ++ [0])) [1]
-- iterate steps:
-- (1) [1]
-- (2) zipWith (+) ([0,1]) ([1,0]) = [1,1]
-- (3) zipWith (+) ([0,1,1]) ([1,1,0]) = [1,2,1]
-- (4) zipWith (+) ([0,1,2,1]) ([1,2,1,0]) = [1,3,3,1]
-- ...
-------------------------------------------------------------------------------
-- steps:
-- (1) [pd !! (1-1) !! 0]
-- (2) [pd !! (2-1) !! 0]
-- (3) [pd !! (3-1) !! 0] ++ [pd !! (3-2) !! 1]
-- (4) [pd !! (4-1) !! 0] ++ [pd !! (4-2) !! 1]
-- (5) [pd !! (5-1) !! 0] ++ [pd !! (5-2) !! 1] ++ [pd !! (5-3) !! 2]
-- (6) [pd !! (6-1) !! 0] ++ [pd !! (6-2) !! 1] ++ [pd !! (6-3) !! 2]
-- (7) [pd !! (7-1) !! 0] ++ [pd !! (7-2) !! 1] ++ [pd !! (7-3) !! 2] ++ [pd !! (7-4) !! 3]
-- ...
-- (n) [pd !! (n-1) !! 0] ++ [pd !! (n-2) !! 1] ++ [pd !! (n-3) !! 2] ++ ...
fibdiag :: Integer -> [Integer]
fibdiag n
| n <= 0 = []
| otherwise = map (\x -> pd `get` (n-x) `get` (x-1)) [1..count]
where
count = (n + 1) `div` 2
-------------------------------------------------------------------------------
fibdiags :: [[Integer]]
fibdiags = map (fibdiag) [1..]
-------------------------------------------------------------------------------
fibspd :: [Integer]
fibspd = map (\x -> sum x) fibdiags
|