diff options
Diffstat (limited to 'lec06/Fibonacci.hs')
-rw-r--r-- | lec06/Fibonacci.hs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/lec06/Fibonacci.hs b/lec06/Fibonacci.hs index bc9e366..375e5ef 100644 --- a/lec06/Fibonacci.hs +++ b/lec06/Fibonacci.hs @@ -1,3 +1,6 @@ +{-# OPTIONS_GHC -fno-warn-missing-methods #-} +-- ^ option for exercise 6 + -- Exercise 1 fib :: Integer -> Integer @@ -46,3 +49,16 @@ evenDiv2 n = toInteger $ length $ takeWhile even $ iterate (`quot` 2) n ruler :: Stream Integer ruler = streamMap evenDiv2 $ streamFromSeed (+1) 1 +-- Exercise 6 (Optional) + +x :: Stream Integer +x = Cons 0 $ Cons 1 $ streamRepeat 0 + +instance Num (Stream Integer) where + fromInteger n = Cons n $ streamRepeat 0 + negate s = streamMap negate s + (+) s1 s2 = streamMap (\p -> fst p + snd p) $ zip s1 s2 + where zip (Cons a0 sa) (Cons b0 sb) = Cons (a0,b0) (zip sa sb) + (*) (Cons a0 sa) b@(Cons b0 sb) = Cons (a0*b0) $ (streamMap (*a0) sb + (sa*b)) + + |