summaryrefslogtreecommitdiff
path: root/lec06/Fibonacci.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lec06/Fibonacci.hs')
-rw-r--r--lec06/Fibonacci.hs15
1 files changed, 15 insertions, 0 deletions
diff --git a/lec06/Fibonacci.hs b/lec06/Fibonacci.hs
index 863e11a..afa2263 100644
--- a/lec06/Fibonacci.hs
+++ b/lec06/Fibonacci.hs
@@ -67,3 +67,18 @@ instance Fractional (Stream Integer) where
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
+