scheme - How to append two streams? -


i have got these functions

(define force!   (lambda (thunk)     (thunk)))   (define stream-head   (lambda (s n)     (if (zero? n)         '()         (cons (car s)               (stream-head (force! (cdr s))                            (1- n))))))  (define make-stream   (lambda (seed next)     (letrec ([produce (lambda (current)                         (cons current                               (lambda ()                                 (produce (next current)))))])       (produce seed))))    (define make-traced-stream   (lambda (seed next)     (letrec ([produce (trace-lambda produce (current)                         (cons current                               (lambda ()                                 (produce (next current)))))])       (produce seed))))  (define stream-of-even-natural-numbers   (make-traced-stream 0                       (lambda (n)                         (+ n 2))))  (define stream-of-odd-natural-numbers   (make-traced-stream 1                       (lambda (n)                         (+ n 2)))) 

and need make function appends last 2 streams, if run

(stream-head (append-stream stream-of-even-natural-numbers stream-of-odd-natural-numbers) 4)  output (0 2 1 3) 

problem is, streams infinite, , dunno how make function knows when stop taking input first stream , continue taking input last.

earlier made merge of 2 lists, looks this;

>  (define (merge-streams s1 s2)  (cons (car s1)  (delay (merge-streams > s2 (force!(cdr s1))))))  (stream-head (merge-stream stream-of-even-natural-numbers stream-of-odd-natural-numbers) 10) = (0 1 2 3 4 5 6 7 8 9) 

here can delay, , alternate element each list take.

how can make smart procedure append lists?

(define (append-streams s1 s2)   (cond     [(empty-stream? s1) s2]      [(empty-stream? s2) s1]     [else       (cons (stream-car s1)             (delay (append-streams (stream-cdr s1) s2)))])) 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

node.js - Using Node without global install -

php - CakePHP HttpSockets send array of paramms -