diff options
Diffstat (limited to 'AufgabeFFP1.hs')
| -rw-r--r-- | AufgabeFFP1.hs | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/AufgabeFFP1.hs b/AufgabeFFP1.hs index 705dce1..648c773 100644 --- a/AufgabeFFP1.hs +++ b/AufgabeFFP1.hs | |||
| @@ -1,24 +1,46 @@ | |||
| 1 | module AufgabeFFP1 | 1 | module AufgabeFFP1 |
| 2 | where | 2 | where |
| 3 | 3 | ||
| 4 | -- calculates 2^ with each element of the continuous list | 4 | ------------------------------------------------------------------------------- |
| 5 | -- Helpers | ||
| 6 | |||
| 7 | -- !! Index function for Integer-type | ||
| 8 | get :: [a] -> Integer -> a | ||
| 9 | get (x:xs) 0 = x | ||
| 10 | get (x:xs) n = get xs (n-1) | ||
| 11 | |||
| 12 | |||
| 13 | ------------------------------------------------------------------------------- | ||
| 14 | |||
| 15 | -- recursive version | ||
| 5 | pof2s :: [Integer] | 16 | pof2s :: [Integer] |
| 6 | pof2s = map (2^) [0,1..] | 17 | pof2s = 1:(map (2*) pof2s) |
| 7 | 18 | ||
| 8 | -- alternate: | 19 | -- alternate: |
| 20 | -- calculates 2^ with each element of the continuous list | ||
| 21 | -- pof2s :: [Integer] | ||
| 22 | -- pof2s = map (2^) [0,1..] | ||
| 23 | |||
| 24 | -- alternate 2: | ||
| 9 | -- pof2s :: [Integer] | 25 | -- pof2s :: [Integer] |
| 10 | -- pof2s = iterate (2*) 1 | 26 | -- pof2s = iterate (2*) 1 |
| 11 | 27 | ||
| 12 | ------------------------------------------------------------------------------- | 28 | ------------------------------------------------------------------------------- |
| 13 | 29 | ||
| 30 | -- recursive version: | ||
| 31 | pd :: [[Integer]] | ||
| 32 | pd = [1] : (zipWith (\x y -> zipWith (+) ([0]++x) (y++[0])) pd pd) | ||
| 33 | |||
| 34 | -- alternate: | ||
| 35 | -- pd :: [[Integer]] | ||
| 36 | -- pd = iterate (\row -> zipWith (+) ([0] ++ row) (row ++ [0])) [1] | ||
| 37 | |||
| 14 | -- iterate steps: | 38 | -- iterate steps: |
| 15 | -- (1) [1] | 39 | -- (1) [1] |
| 16 | -- (2) zipWith (+) ([0,1]) ([1,0]) = [1,1] | 40 | -- (2) zipWith (+) ([0,1]) ([1,0]) = [1,1] |
| 17 | -- (3) zipWith (+) ([0,1,1]) ([1,1,0]) = [1,2,1] | 41 | -- (3) zipWith (+) ([0,1,1]) ([1,1,0]) = [1,2,1] |
| 18 | -- (4) zipWith (+) ([0,1,2,1]) ([1,2,1,0]) = [1,3,3,1] | 42 | -- (4) zipWith (+) ([0,1,2,1]) ([1,2,1,0]) = [1,3,3,1] |
| 19 | -- ... | 43 | -- ... |
| 20 | pd :: [[Integer]] | ||
| 21 | pd = iterate (\row -> zipWith (+) ([0] ++ row) (row ++ [0])) [1] | ||
| 22 | 44 | ||
| 23 | ------------------------------------------------------------------------------- | 45 | ------------------------------------------------------------------------------- |
| 24 | 46 | ||
| @@ -33,16 +55,16 @@ pd = iterate (\row -> zipWith (+) ([0] ++ row) (row ++ [0])) [1] | |||
| 33 | -- ... | 55 | -- ... |
| 34 | -- (n) [pd !! (n-1) !! 0] ++ [pd !! (n-2) !! 1] ++ [pd !! (n-3) !! 2] ++ ... | 56 | -- (n) [pd !! (n-1) !! 0] ++ [pd !! (n-2) !! 1] ++ [pd !! (n-3) !! 2] ++ ... |
| 35 | fibdiag :: Integer -> [Integer] | 57 | fibdiag :: Integer -> [Integer] |
| 36 | fibdiag (n) | 58 | fibdiag n |
| 37 | | n <= 0 = [] | 59 | | n <= 0 = [] |
| 38 | | otherwise = map (\x -> pd !! (fromInteger n - x) !! (x - 1)) [1..count] | 60 | | otherwise = map (\x -> pd `get` (n-x) `get` (x-1)) [1..count] |
| 39 | where | 61 | where |
| 40 | count = (fromInteger n + 1) `div` 2 | 62 | count = (n + 1) `div` 2 |
| 41 | 63 | ||
| 42 | ------------------------------------------------------------------------------- | 64 | ------------------------------------------------------------------------------- |
| 43 | 65 | ||
| 44 | fibdiags :: [[Integer]] | 66 | fibdiags :: [[Integer]] |
| 45 | fibdiags = map (fibdiag) [1,2..] | 67 | fibdiags = map (fibdiag) [1..] |
| 46 | 68 | ||
| 47 | ------------------------------------------------------------------------------- | 69 | ------------------------------------------------------------------------------- |
| 48 | 70 | ||
