From aa9625ea413f9c7f191aefc75f29c74e7dea0b9f Mon Sep 17 00:00:00 2001 From: Edoardo La Greca Date: Thu, 28 Aug 2025 20:47:30 +0200 Subject: add first part of sixth exercise of lecture 6 --- lec06/Fibonacci.hs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'lec06/Fibonacci.hs') 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)) + + -- cgit v1.2.3