<jupyter_start><jupyter_text>title<jupyter_code>(define (stream-enumerate-interval low high)
  (if (> low high)
      the-empty-stream
      (cons-stream 
       low
       (stream-enumerate-interval (+ low 1) high))))

(define integers (stream-enumerate-interval 1 50))



(define (stream-map proc . argstreams)
  (if (null? (car argstreams))
      the-empty-stream
      (cons-stream
       (apply proc (map (lambda (x) (stream-car x)) argstreams))
       (apply stream-map
	      (cons proc (map (lambda (x) (stream-cdr x)) argstreams))))))


(define (add-streams s1 s2)
  (stream-map + s1 s2))

(define (stream-for-each proc s)
  (if (stream-null? s)
      'done
      (begin (proc (stream-car s))
	     (stream-for-each proc (stream-cdr s)))))


(define (display-stream s)
  (stream-for-each display-line s))




(define (add-streams s1 s2)
  (stream-map + s1 s2))

(define (mul-streams s1 s2)
  (stream-map * s1 s2))

(define factorials (cons-stream 1 (mul-streams integers factorials)))

(define (partial-sums s)
  (define result-s (cons-stream (stream-car s) (add-streams (stream-cdr s) result-s)))
  result-s)

(define (scale-stream stream factor)
  (stream-map (lambda (x) (* x factor)) stream))

(define (merge s1 s2)
  (cond ((stream-null? s1) s2)
	((stream-null? s2) s1)
	(else
	 (let ((s1car (stream-car s1))
	       (s2car (stream-car s2)))
	   (cond ((< s1car s2car)
		  (cons-stream s1car (merge (stream-cdr s1) s2)))
		 ((> s1car s2car)
		 (cons-stream s2car (merge s1  (stream-cdr s2))))
	   (else
	    (cons-stream s2car
			 (merge (stream-cdr s1)
				(stream-cdr s2)))))))))

(define fibs
  (cons-stream 0
	       (cons-stream 1
			    (add-streams (stream-cdr fibs) fibs))))

(define (start-test-3-57)

  (display "we need n plus to caculate fibs(n) with the stream style difination") (newline)
  (display "if we use lambda to replace delay, we need to re-caculate each number to get fibs(n), as lambda method doesn't keep the result") (newline) 
  
  (define testing fibs)
  
  (display "elements 0 is: ") (display (stream-ref testing 0)) (newline)
  
  (display "elements 1 is: ") (display (stream-ref testing 1)) (newline)
  (display "elements 2 is: ") (display (stream-ref testing 2)) (newline)
  (display "elements 3 is: ") (display (stream-ref testing 3)) (newline)
  (display "elements 4 is: ") (display (stream-ref testing 4)) (newline)
  (display "elements 5 is: ") (display (stream-ref testing 5)) (newline)
  (display "elements 6 is: ") (display (stream-ref testing 6)) (newline)

)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup