memq for lists and streams

Viewed 65

Could someone tell me what potential problems we would get from changing these two first functions dedicated to removing duplicates from a list to the last two that are for streams

(define (memq item x)
 (cond ((null? x ) #f)
  ((eq? item (car x )) x)
    (else (mem item (cdr x)))))


(define (r lst)
 (cond ((null? lst) '())
   ((not (memq (car lst ) (scdr lst)))
    (cons(car lst) (r (cdr lst))))
     (else (r ( cdr last)))))

Under we have the stream version, all I have done here as you can see is changing car,cons and cdr to stream versions. I read that this will have potential problems but I cannot see why.

(define (memq item x)
 (cond ((null? x ) #f)
  ((eq? item (stream-car x )) x)
    (else (memq item (stream-cdr x)))))


(define (r lst)
 (cond ((null? lst) '())
   ((not (memq (stream-car lst ) (stream-cdr lst)))
    (cons-stream (stream-car lst) (r (stream-cdr lst))))
     (else (r ( stream-cdr lst)))))
1 Answers

A big part of the point of streams is that they may potentially be infinite, because they are generated as needed. When the stream version of your r function is called on any infinite stream, it will never produce a single value. Can you see why this is?

Related