This question is about programming in DrRacket Scheme. How to write the codes fo
ID: 3725850 • Letter: T
Question
This question is about programming in DrRacket Scheme. How to write the codes for this question?
Question 5 [13 marks total] For the following stream related questions you may begin with these definitions as well as those found in the lecture notes a. [5 marks] Write stream analogues of some familiar list processing functions: i. [1 marks] (strcam-first n str) makes a new stream of the first n items in str ii. [2 marks] (list->stream lis) -- makes a stream from list lis iii. [2 marks] (stream->list str) - reverse of above (assume str is finitc) b. [6 marks] Write stream generators to help test the above functions i. an infinite stream of l's ii. an infinite stream of all odd integers iii. an infinite stream of the values of function f(n) - f(n-1) + 2(n-2) + 3f(n-3) (given f(n)-n iff nc4). c. [2 marks] Write a function combine that takes two streams and combines them using a function passed in as a third parameter. You may assume the both streams are ofequal length (or infinite). (combine+ (1 2 3 ) (4 5 6 )) (5 7 9 ..)Explanation / Answer
a.ANS:
procedure: (stream-ref stream n)
{} × nat
Stream-ref returns the nth element of stream, counting from zero. An error is signaled if n is greater than or equal to the length of stream.
(define (fact n)
(stream-ref
(stream-scan * 1 (stream-from 1))
n))
(stream-filter odd? (stream-from 0))
1 3 5 7 9 ...
iii. (define-stream (stream-map proc strm)
(if (stream-null? strm)
stream-null
(stream-cons
(proc (stream-car strm))
(stream-map proc (stream-cdr strm))))))
(stream-range 0 10) 0 1 2 3 4 5 6 7 8 9
(stream-range 0 10 2) 0 2 4 6 8
v. procedure: (list->stream list-of-objects)
[] {}
List->stream takes a list of objects and returns a newly-allocated stream containing in its elements the objects in the list. Since the objects are given in a list, they are evaluated when list->stream is called, before the stream is created. If the list of objects is null, as in (list->stream '()), the null stream is returned. See also stream.
(define strm123 (list->stream '(1 2 3)))
; fails with divide-by-zero error
(define s (list->stream (list 1 (/ 1 0) -1)))
(stream->list 10
(stream-map (lambda (x) (* x x))
(stream-from 0)))
(0 1 4 9 16 25 36 49 64 81)
define the stream of integers?
(define integers (cons-stream 0 (add-stream integers ones)))
Use filter to define odd-integers, the stream of odd integers.
(define odd-integers (stream-filter odd? integers))
Note that (stream-filter even? odd-integers) will result in an infinite loop.
We obtain a stream with always the same number. This is similar to the definition of ones. (define (make-random-stream) (cons-stream (random 100) (make-random-stream)))
We obtain a stream of different random numbers. If the stream is memoized, the sequence of numbers stays the same. If we do not memoize, then the value of a particular element in the stream will be different each time we read it with stream-car.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.