summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lec06/Fibonacci.hs20
1 files changed, 20 insertions, 0 deletions
diff --git a/lec06/Fibonacci.hs b/lec06/Fibonacci.hs
index 375e5ef..afa2263 100644
--- a/lec06/Fibonacci.hs
+++ b/lec06/Fibonacci.hs
@@ -61,4 +61,24 @@ instance Num (Stream Integer) where
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))
+
+-- Exercise 7 (Optional)
+
+data Matrix = M Integer Integer Integer Integer
+
+instance Num Matrix where
+ (*) (M a11 a12 a21 a22) (M b11 b12 b21 b22) = M (a11*b11+a12*b21) (a11*b12+a12*b22) (a21*b11+a22*b21) (a21*b12+a22*b22)
+
+fib4 :: Integer -> Integer
+fib4 0 = 0
+fib4 n = take12 ((M 1 1 1 0)^n)
+ where take12 (M a11 a12 a21 a22) = a12
+
+fibs4 :: Stream Integer
+fibs4 = streamMap fib4 $ streamFromSeed (+1) 0