summaryrefslogtreecommitdiff
path: root/lec06/Fibonacci.hs
diff options
context:
space:
mode:
authorEdoardo La Greca2025-08-28 20:47:30 +0200
committerEdoardo La Greca2025-08-28 20:47:30 +0200
commitaa9625ea413f9c7f191aefc75f29c74e7dea0b9f (patch)
tree470fb2289356f50f9dfffa77542ea9256bade749 /lec06/Fibonacci.hs
parent79d021f510d15c67e534dd8f54c5c603b62d61b4 (diff)
add first part of sixth exercise of lecture 6HEADmaster
Diffstat (limited to 'lec06/Fibonacci.hs')
-rw-r--r--lec06/Fibonacci.hs16
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))
+
+