4. (10 pts) Explain what the following Scheme code is doing (define (make-stream
ID: 3600277 • Letter: 4
Question
4. (10 pts) Explain what the following Scheme code is doing (define (make-stream n f) (define (next m) (next n)) (define head car) (define (tail stream) ((cdr stream))) (define (nth stream n) (if (- n 0) (head stream) (nth (tail stream) ( n 1)))) (define even (make-stream 0 (lambda (n) (+ n 2)))) Try it out in Scheme48 and check the values of the following expressions: even (head even) (head (tail even)) (head (tail (tail even)) (head (tail (tail (tail even)) )) (nth even 5) (nth even 1000) Explain what the lambda in make-stream is good for, where this function is called, and how tail and nth work. To see what's going on, trace manually through the execution of (head (tail (tail even))Explanation / Answer
#include
using namespace std;
int main(void)
{
// structure:
// while ( condition )
// statement;
// do
// statement;
// while ( condidion );
// 1 counted loop
// 1.1 input count
int lowRange;
int highRange;
while (true)
{
cout << "Enter a Numerical Range with a low and high bound: ";
cin >> lowRange>> highRange;
cin.ignore(999, ' ');
if (cin.fail())
{
cin.clear();
cin.ignore(999, ' ');
cout << "data format error, try again" << endl;
continue;
}
if ( lowRange >= 0)
break; // end test
cout << "no negative numbers, try again" << endl;
}
// 1.2
int howManyTimesAccomplished = 0;
while (howManyTimesAccomplished < lowRange)
{
// 1.2.1 output greeting
cout << "Hello, World!" << endl;
// howManyTimesAccomplished = howManyTimesAccomplished + 1;
// howManyTimesAccomplished += 1;
++howManyTimesAccomplished; // howManyTimesAccomplished++
}
// 1.3 output greetings
for (int sumRange = 0
; int sumTwoRange = 1
; sumTwoRange <= highRange
;)
cout << "Hello, World!" << endl;
cout << endl;
// 2 more greetings (not counted):
cout << "more greetings" << endl;
while (true)
{
// 2.1 output greeting
cout << "Hello World" << endl;
// 2.2 ask if user wants to do again
bool doAgain;
while (true)
{
char userResponse;
cout << "Do you want to do it again? ";
cin >> userResponse;
cin.ignore(999, ' ');
if (cin.fail())
{
cin.clear();
cin.ignore(999, ' ');
cout << "Please enter y or n." << endl;
continue;
}
if (userResponse == 'y' || userResponse == 'Y')
{
doAgain = true;
break;
}
if (userResponse == 'n' || userResponse == 'N')
{
doAgain = false;
break;
}
cout << "Please enter y or n." << endl;
}
if (!doAgain) break;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.