1

The questions is: Write a function (repeat x) which, given a value x, produces a stream that repeats x ad infinitum. For instance, the call (take (repeat 42) 5) produces (42 42 42 42 42).

My problem is that my repeat function doesn't know what to stop... so it runs forever.

This is the code I have so far.

#lang R5RS
;; Repeat your solution for take
(define (integer-stream x)
    (cons x  (delay (integer-stream (+ 1 x)))))
(define nat (integer-stream 0))

(define (stream-car stream)
  (car stream))

(define (stream-cdr stream)
  (force (cdr stream)))

(define (head stream)  (car stream))
(define (rest stream)  (force (cdr stream)))

(define (take s k)
  (cond ((= k 0) '())
        (else (cons (car s) (take (stream-cdr s) (- k 1))))))

;;;;;;;;;
(define (stream-map f s)
  (if (null? s) '()
      (cons (f (car s))
            (delay (stream-map f (rest s))))))

(define (squares x)
  (* x x))

(define (square s)
  (cond ((null? s) '())
        (else (stream-map squares s))))

;;;;;;;;;;;
(define (cubess x)
  (* x x x))

(define (cube s)
  (cond ((null? s) '())
        (else (stream-map cubess s))))

;;;;
(define (merge-streams s t)
  (cond ((null? s) t)
        ((null? t) s)
        ((< (stream-car s) (stream-car t))
         (cons (stream-car s)
               (delay (merge-streams (stream-cdr s) t))))
        (else (cons (stream-car t) (delay (merge-streams s (stream-cdr t)))))))

;;;;;

(define (repeat x)
  (cons x (repeat x)))
Moronis2234
  • 57
  • 1
  • 6
  • 1
    It's supposed to repeat infinitely, that's what *ad infinitum* means. – Barmar Apr 29 '22 at 19:37
  • @Barmar but this command >(take (repeat 42) 5) produces (42 42 42 42 42) so wouldn't that mean it doesn't repeat forever? When I run this function it tell's me im out of memory. – Moronis2234 Apr 29 '22 at 19:56
  • `take` puts a finite bound on the number of items generated from an infinite stream. – Shawn Apr 29 '22 at 19:59
  • look into your definition of `integer-stream`, and do the same thing in your `repeating-stream` (or "`repeat`") definition. that's how you get what the answer is saying. – Will Ness Apr 29 '22 at 23:12
  • 1
    also, `(take 1 s)` should *not* force `s`'s tail (which would entail forcing the next element needlessly, which could possibly cause an error, like `(take 3 {1/3, 1/2, 1/1, 1/0, 1/(-1), ...})`). – Will Ness Apr 30 '22 at 09:46

1 Answers1

1

You're close- you just have to use delay in your repeat function:

(define (repeat arg)
  (cons arg (delay (repeat arg))))

Rest of code should be the same- so this will be the full code:

#lang r5rs

(define (stream-car stream)
  (car stream))

(define (stream-cdr stream)
  (force (cdr stream)))

(define (take stream n)
  (if (zero? n) '()
      (cons (stream-car stream)
            (take (stream-cdr stream) (- n 1))))) 

(define (repeat arg)
  (cons arg (delay (repeat arg))))

Test:

> (repeat 42)
(42 . #<promise>)

> (take (repeat 42) 5)
(42 42 42 42 42)
Martin Půda
  • 7,353
  • 2
  • 6
  • 13
  • do you have any input on this function: ```(define (weave s1 s2) (cond ((null? s1) s1) ((null? s2) s2) (#t (cons (stream-car s1) (cons (stream-car s2) (delay(weave (stream-cdr s1) (stream-cdr s2)))))))) ```It's doing the same thing (running forever) even though I did as you said - added a delay. – Moronis2234 Apr 29 '22 at 21:06
  • @Zoxze please post new question with full code as relevant to the new code you are asking about. – Will Ness Apr 29 '22 at 23:11
  • OP also has a very common error in their `take` function. – Will Ness Apr 29 '22 at 23:11
  • i.e. [this](https://stackoverflow.com/questions/72062900/how-would-i-implement-a-repeat-function-that-would-make-a-stream-full-of-a-repea#comment127338080_72062900). – Will Ness Apr 30 '22 at 09:45