<< Previous exercise (3.67)
| Index |
Next exercise (3.69) >>
no. The program will infinitely loop. because their is no delay in (pairs (stream-cdr s) (stream-cdr t)), it will be called recursively.
I disagree @meteorgan. interleave returns a cons-stream, then it has delay. Anyway, I think this solution won't work either, because it will produce the following sequence:
(1,1) (2,2) (1,2) (3,3) (1,3) (4,4) (1,4) (5,5)....
The recursive call is always going to call to the second element of interleave, because there is no 'pivot' to do an interleaving, thus it leaves out elements like (3,4) (3,5)...(4,5) (4,6)... and so on.
@meteorgan is correct here. interleave returns a cons-stream, but interleave will never be successfully called since its second arg (pairs ...) will be evaluated first. Attempting this evaluation will result in recursion sans base-case.
Louis' implementation will recurse infinitely simply because interleave is an ordinary *function*, not a special form like cons-stream, and hence will need to fully evaluate both arguments first, since Scheme uses eager evaluation for ordinary functions. Since the second argument to interleave is a recursive call to pairs, and there is no hard-coded base case, this implementation will recurse infinitely.
I agree with posts above about infinite loop.
However, for me the pairs procedure also fails with finite streams such as (stream-enumerate-interval 1 10) - I get a contract violation error message - perhaps there is another problem with code too?