Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The code is written in Scheme. Need help with all the stream questions in the picture, below is the scan code need to modify for
The code is written in Scheme.
Need help with all the stream questions in the picture, below is the scan code need to modify for problem 1.
(define (scan f z l) (if (null? l) (cons z l) (cons z (scan f (f z (first l)) (rest l)))))
Problem 1: (stream-scan f z s) (10 points) Modify your scan function from Homework 1 so that it takes a stream as an argument and returns a stream as the result, stream-scan must not use lists. Problem 2: (stream-take-n n s) (10 points) Write a function stream-take-n that takes two arguments, an integer n and a stream s, and returns a list consisting of the first n items in the stream. Problem 3: (stream-pair-with f s) (10 points) Write a function stream-pair-with that takes a function f and a stream s, consisting of elements X1,42, ---, and returns a new stream where each element x; of s has been paired with fx Examples: (stream->list (stream-pair-with (Lambda (x) (+ x 1)) (stream 1 2 3 4))) => ((1.2) (2 . 3) (3 . 4) (4.5)) Problem 4: (cycle-lists xs ys) (10 points) Write a function cycle-lists that takes two lists, xs and ys, and returns a stream. The lists may or may not be the same length, but you may assume they are both non-empty. The elements produced by the stream are pairs where the first part is from XS and the second part is from ys. The stream cycles forever through the lists. Your solution should not require the use of mutable state, i.e., it should not use set! . A constant-time (O(1)) solution is possible. Examples: (stream-take-n 8 (cycle-lists '(1 2 3) ("a" "b"))) => ((1. "a") (2 . "b") (3, "a") (1, "b") (2. "a") (3, "b") (1. "a") (2. "b")) Hint: Think about how you could create a stream that endlessly cycles through all the elements of a single, given list. You can use a local environment to store a copy of the list through which you must cycle and a helper function that uses this stored copy when it runs out of elements to cycle through. Problem 5: (seen x) (10 points) Write a function seen that takes a single argument x and return #t if it has been previously called with an equal? argument and #f otherwise. Hint: You may use a global variable if you like, but a solution that does not use global state is possible. Problem 1: (stream-scan f z s) (10 points) Modify your scan function from Homework 1 so that it takes a stream as an argument and returns a stream as the result, stream-scan must not use lists. Problem 2: (stream-take-n n s) (10 points) Write a function stream-take-n that takes two arguments, an integer n and a stream s, and returns a list consisting of the first n items in the stream. Problem 3: (stream-pair-with f s) (10 points) Write a function stream-pair-with that takes a function f and a stream s, consisting of elements X1,42, ---, and returns a new stream where each element x; of s has been paired with fx Examples: (stream->list (stream-pair-with (Lambda (x) (+ x 1)) (stream 1 2 3 4))) => ((1.2) (2 . 3) (3 . 4) (4.5)) Problem 4: (cycle-lists xs ys) (10 points) Write a function cycle-lists that takes two lists, xs and ys, and returns a stream. The lists may or may not be the same length, but you may assume they are both non-empty. The elements produced by the stream are pairs where the first part is from XS and the second part is from ys. The stream cycles forever through the lists. Your solution should not require the use of mutable state, i.e., it should not use set! . A constant-time (O(1)) solution is possible. Examples: (stream-take-n 8 (cycle-lists '(1 2 3) ("a" "b"))) => ((1. "a") (2 . "b") (3, "a") (1, "b") (2. "a") (3, "b") (1. "a") (2. "b")) Hint: Think about how you could create a stream that endlessly cycles through all the elements of a single, given list. You can use a local environment to store a copy of the list through which you must cycle and a helper function that uses this stored copy when it runs out of elements to cycle through. Problem 5: (seen x) (10 points) Write a function seen that takes a single argument x and return #t if it has been previously called with an equal? argument and #f otherwise. Hint: You may use a global variable if you like, but a solution that does not use global state is possibleStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started