Per this homework, I'm trying to implement a Fractional (Stream Integer)
where the stream represents coefficients of polynomials:
where Q is defined as Q = (A'/b0) + x((1/b0)(A' − QB0)).
where A/B = Q,
That is, the first element of the result is A'/b0; the remainder is formed by computing A' − QB' and dividing each of its elements by b0
Note that A' == tail of A and same for B'
Here's my attempt:
divStreams :: Stream Integer -> Stream Integer -> Stream Integer
divStreams a@(Cons x xs) b@(Cons y ys) =
Cons (x `div` y) (streamMap ((1 `div` y) *) xs - (multStreams q ys) )
where q = divStreams a b
with the following functions' definitions:
multStreams :: Stream Integer -> Stream Integer -> Stream Integer
multStreams (Cons x xs) b@(Cons y ys) =
Cons (x * y) (streamMap (*x) ys + multStreams xs b)
data Stream a = Cons a (Stream a)
streamMap :: (a -> b) -> Stream a -> Stream b
streamMap f (Cons x xs) = Cons (f x) rest
where rest = streamMap f xs
Note that I defined Num (Stream Integer)
where (+)
== combineStreams (+)
combineStreams :: (a -> b -> c) -> Stream a -> Stream b -> Stream c
combineStreams f (Cons x xs) (Cons y ys) = Cons (f x y) rest
where rest = combineStreams f xs ys
Thanks to this answer that resolved an issue I had implementing multiplication of Streams.