d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Emacs - Scheme > How To Merge Two Functions
Add Reply New Topic New Poll
Member
Posts: 3,141
Joined: Jul 16 2005
Gold: 224,068.45
May 21 2015 08:59am
Hi.
I was hoping someone might be able to help me on this one

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 I need to make a function that merges the last two, so that if I run
(stream-head (merge-streams stream-of-even-natural-numbers stream-of-odd-natural-numbers) 10)
I get the output (0 1 2 3 4 5 6 7 8 9).. how is this done?
Member
Posts: 3,141
Joined: Jul 16 2005
Gold: 224,068.45
May 21 2015 09:00am
My own solution, which is wrong. Looks like this.

(define merge-streams
(lambda(x y)
(cons (car x)
(merge-streams y (cdr x)))))

And basically I do not know why it is not correct

Go Back To Programming & Development Topic List
Add Reply New Topic New Poll