Let's try it out. Primes takes, let's take the first prime

numbers and convert it to a list. And there you go.

The first prime numbers here on the right. So, let's get back to square roots one

more time. The previous algorithms we've seen for the

square root algorithm always mixed determination criterion is good enough

with the method to calculate new approximations.

With streams at our disposal, we can now express the concept of a converging

sequence without having to worry about determination criteria.

So, we could write a stream that represents all the successive

approximations of, of a square root of a given number.

The thing we would do is we would have the usual improved method.

And then, we would say, well, we have a lazy val guesses which is a stream of

double, and the guesses start with one. And each successive value is obtained by

mapping the improved function over guesses.

So, this might look highly alarming to you, a little bit like a snake eating it's

tail, because what happens here is that you apply a map operation on the value

that you're about to define. So, won't this blow up in an infinite

recursion? Well, again, no.

Because we have the operation stream cons here, which is lazy in its right operand

So, everything actually works out as it should.

You can convince yourself by going to the worksheet.

I have given you precisely the same stream method.

So, let's try to apply this to a number. Let's say four.

Take the first ten elements and print it out.

So, what we would get is a perfectly good stream of approximations to the number

four, Which after a couple of iterations,

converges to 2.0. So now, we have decoupled the idea of a

converging sequence from determination criterion.

Of course, we can add determination criterion later.

We see that here. So, we have to find the is good enough

function as before, and we can then subject our square root stream to a filter

method of all the elements that are good enough approximations.

So, let's do that in the worksheet. I've added a good enough method and then,

let me just filter that. Filter is good enough with respect to my

number four. And again, I want to show, again, I want

to show the first ten elements. So, what you see now is you get a stream

of solutions that are all already good enough approximations to the actual square

root value, so you could just take the first one of that stream, and return that

as the result of your computation. So, here's a quiz for you.

Consider the following two equivalent ways to define an infinite stream of multiples

of a given number N. You could write, from one map times n, as

we've seen, But you could also write from one filter

modular N equals zero. Both expressions would give you the stream

of multiples of N. But which of the two expressions generates

its results faster? Would it be that expression here or the

filter down there? So, let's see how these two expressions

would behave. Let's take, for the sake of the example, N

equals three. So, the first expression would produce,

first, all the elements of the natural numbers starting from one, and immediately

multiply each of these by three. So, it would yield, in that order, three,

six, nine, twelve, and so on. The second expression would produce all

natural numbers as before. And then, it would filter these numbers to

keep only those that are divisible by three.

So, it would keep the filter operations that would now keep every third number in

the stream. So, by that argument, the map operation is

more efficient because it doesn't generate unnecessary stream elements that are

filtered out afterwards. So, my vote goes to answer one.