Question

I would like to have a stream in scheme that holds a bunch of matrices that have a certain order.

The stream-car of this stream would be the matrix [1 6 0 3]; that is, row 1 col 1 is 1, row 1 col 2 is 6, row 2 col 1 is 0, and row 2 col 2 is 3. Each matrix is technically a list, but I have a representation (constructor and selectors) for a 2x2 matrix. So, this will be a stream of 2x2 matrices.

Now, the next item in the stream should be [2 10 0 5]. The pattern here is that the matrices in the next stream increases by the following: [k (4k+2) 0 (2k+1)] where k is the kth matrix.

I have an idea how I want to store these. As an example, I know that I can get a continuous stream of ones with:

(define ones (cons-stream 1 ones))

and a continuous stream of integers with:

(define integers (cons-stream 1 (add-streams ones integers)))

So, I would like a continuous stream of matrices that are in the format described above. That is, the first one (the car-stream) will be a matrix represented by [1 6 0 3] then a matrix represented by [2 10 0 5] then a matrix represented by [3 14 0 7].

So, I know it will be something like:

(define start-matrix '(1 6 0 3))

(define init-stream (cons-stream start-matrix 
                             (add-streams ___________
                                          init-stream)))

The underlined is what "I think" is the missing piece. I've removed the "add-streams" procedure from this post to clear my post up.

***EDIT: Realized I think my "start-matrix" has to be 1 6 0 3, not 1 4 0 2.

But there has to be a way to add 1 4 0 2 to the kth matrix.

Was it helpful?

Solution

Ok, firstly, you realise you're going to have to define your own version of add-streams to do the matrix addition? (I realise this may have been in what you edited away).

Secondly, can you not see what is missing between the SICP example and your version? In the SICP example, there is a function which provides a constant stream of 1s. 1 is what is added to each new element of the integers stream. Now, there's something you want to add to each new member of the init-stream stream. Surely you can work out what that is? I mean, if you add it ever time and you do it k times... (you mention it so many times, you must know what it is). So all you are missing is a function that delivers an endless stream of that one thing.

Thirdly, do you realise why the SICP example adds new elements from the ones stream, rather than just adding 1 each time? (There's a principle being demonstrated).

Fourthly, do you see that init-stream is not a good name for this function? init-stream indicates a general purpose function, whereas you are defining something that returns a very specific stream. Why not just follow the SICP naming examples and name it for what it returns?

My third and fourth questions needn't be answered for you to solve your problem; I'm just interested to know if you grasp the point.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top