summaryrefslogtreecommitdiffstats
path: root/AufgabeFFP1.hs
diff options
context:
space:
mode:
authorMatthias Wisniowski <matthias.wisniowski@gmail.com>2012-03-24 16:29:24 +0100
committerMatthias Wisniowski <matthias.wisniowski@gmail.com>2012-03-24 16:29:24 +0100
commit190dea09058bebd071fdb107d23834efefcdc030 (patch)
tree6539971fcf5b6e632c43b153f96197aebe53b063 /AufgabeFFP1.hs
parent431e5c6cbc7d6fa745d841d7943ae4b1f63901e4 (diff)
downloadffp-190dea09058bebd071fdb107d23834efefcdc030.tar.gz
ffp-190dea09058bebd071fdb107d23834efefcdc030.tar.bz2
ffp-190dea09058bebd071fdb107d23834efefcdc030.zip
implemented recursive versions of pof2s and pd
Diffstat (limited to 'AufgabeFFP1.hs')
-rw-r--r--AufgabeFFP1.hs38
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 @@
1module AufgabeFFP1 1module AufgabeFFP1
2where 2where
3 3
4-- calculates 2^ with each element of the continuous list 4-------------------------------------------------------------------------------
5-- Helpers
6
7-- !! Index function for Integer-type
8get :: [a] -> Integer -> a
9get (x:xs) 0 = x
10get (x:xs) n = get xs (n-1)
11
12
13-------------------------------------------------------------------------------
14
15-- recursive version
5pof2s :: [Integer] 16pof2s :: [Integer]
6pof2s = map (2^) [0,1..] 17pof2s = 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:
31pd :: [[Integer]]
32pd = [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-- ...
20pd :: [[Integer]]
21pd = 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] ++ ...
35fibdiag :: Integer -> [Integer] 57fibdiag :: Integer -> [Integer]
36fibdiag (n) 58fibdiag 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
44fibdiags :: [[Integer]] 66fibdiags :: [[Integer]]
45fibdiags = map (fibdiag) [1,2..] 67fibdiags = map (fibdiag) [1..]
46 68
47------------------------------------------------------------------------------- 69-------------------------------------------------------------------------------
48 70