Feedback You may specify any length of head to a stream, then repeat 

Forum: Com Sci 12500, U Chicago, Autumn 2002, Discuss Classwork
Re: News Project Step #3, deadline Monday 18 November (Mike O'Donnell)
Re: Question How to create streams recursively? (Ivan Beschastnikh)
Date: 2002, Nov 19
From: Mike O'Donnell <odonnell@cs.uchicago.edu>


(define repeat_1_2_3

  (cons-stream 1 (cons-stream 2 (cons-stream 3 repeat_1_2_3))))
defines the stream of values 1 2 3 repeating infinitely. It's not hard to define a recursive procedure that, given an arbitrarily long list, creates the stream repeating it infinitely. Given the repetition, you can always cons-stream more nonrepeating stuff on the head.

But I don't recommend finding the repeating part of the expansion of a rational explicitly, and then using this technique. It's easier to define a recursive procedure that produces the whole stream uniformly, without noticing explicitly that it repeats. The general form is


(define (sfn n1 ... ni)

  (cons-stream ??? (sfn ???)))
The first ??? is some expression that uses the variables n1 ... ni to describe the first element in the stream. The second ??? is a sequence of expressons to describe the arguments to sfn that produce the remainder of the stream.

For rational->xreal in the tristream implementation, you can probably do this nicely with two variables n1 and n2, giving the numerator and denominator of a fraction to be expanded. Or, you can do it with one rational, but I like to see the numerator and denominator explicitly.




Mike O'D.


to: "You may specify any length of head to a stream, then repeat"