diff options
Diffstat (limited to 'lec06')
-rw-r--r-- | lec06/Fibonacci.hs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/lec06/Fibonacci.hs b/lec06/Fibonacci.hs index bc9e366..863e11a 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,21 @@ 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)) + +instance Fractional (Stream Integer) where + (/) a@(Cons a0 sa) b@(Cons b0 sb) = Cons (a0 `div` b0) $ streamMap (`div` b0) (sa-(a/b)*sb) + +fibs3 :: Stream Integer +fibs3 = x / (1 - x - (x*x)) + |