I see at least two different implementations:
def add_streams(s1:Stream[Int], s2:Stream[Int]): Stream[Int] = Stream.cons(s1.head + s2.head, add_stream(s1.tail, s2.tail))
def add_streams(s1:Stream[Int], s2:Stream[Int]) =
(s1 zip s2) map {case (x,y) => x + y}
I guess the last one is more efficient since it is not recursive.
Is it correct? How would you code such a function ?
The first version is broken as it doesn‘t check for the end of a
Stream. (The streams needn’t be of different length for this to happen.) Given that, thezipversion is the one to prefer.