From 190dea09058bebd071fdb107d23834efefcdc030 Mon Sep 17 00:00:00 2001 From: Matthias Wisniowski Date: Sat, 24 Mar 2012 16:29:24 +0100 Subject: implemented recursive versions of pof2s and pd --- AufgabeFFP1.hs | 38 ++++++++++++++++++++++++++++++-------- 1 file 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 @@ module AufgabeFFP1 where --- calculates 2^ with each element of the continuous list +------------------------------------------------------------------------------- +-- 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 = map (2^) [0,1..] +pof2s = 1:(map (2*) pof2s) -- alternate: +-- calculates 2^ with each element of the continuous list +-- pof2s :: [Integer] +-- pof2s = map (2^) [0,1..] + +-- 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] -- ... -pd :: [[Integer]] -pd = iterate (\row -> zipWith (+) ([0] ++ row) (row ++ [0])) [1] ------------------------------------------------------------------------------- @@ -33,16 +55,16 @@ pd = iterate (\row -> zipWith (+) ([0] ++ row) (row ++ [0])) [1] -- ... -- (n) [pd !! (n-1) !! 0] ++ [pd !! (n-2) !! 1] ++ [pd !! (n-3) !! 2] ++ ... fibdiag :: Integer -> [Integer] -fibdiag (n) +fibdiag n | n <= 0 = [] - | otherwise = map (\x -> pd !! (fromInteger n - x) !! (x - 1)) [1..count] + | otherwise = map (\x -> pd `get` (n-x) `get` (x-1)) [1..count] where - count = (fromInteger n + 1) `div` 2 + count = (n + 1) `div` 2 ------------------------------------------------------------------------------- fibdiags :: [[Integer]] -fibdiags = map (fibdiag) [1,2..] +fibdiags = map (fibdiag) [1..] ------------------------------------------------------------------------------- -- cgit v1.2.3