Question

In: Computer Science

Explain what the following Scheme code is doing: (define (make-stream n f) (define (next m) (cons...

Explain what the following Scheme code is doing:

(define (make-stream n f)
  (define (next m)
    (cons m (lambda () (next (f 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)))

Solutions

Expert Solution

ANSWER:

When a function is called, the computer must "remember" the place it was called from, the return address, so that it can return to that location with the result once the call is complete. Typically, this information is saved on the call stack, a simple list of return locations in order of the times that the call locations they describe were reached. For tail calls, there is no need to remember the place we are calling from – instead, we can perform tail call elimination by leaving the stack alone (except possibly for function arguments and local variables[3]), and the newly called function will return its result directly to the original caller. Note that the tail call doesn't have to appear lexically after all other statements in the source code; it is only important that the calling function return immediately after the tail call, returning the tail call's result if any, since the calling function will never get a chance to do anything after the call if the optimization is performed.

For non-recursive function calls, this is usually an optimization that saves little time and space, since there are not that many different functions available to call. When dealing with recursive or mutually recursive functions where recursion happens through tail calls, however, the stack space and the number of returns saved can grow to be very significant, since a function can call itself, directly or indirectly, creating a new call stack frame each iteration. In fact, it often asymptotically reduces stack space requirements from linear, or O(n), to constant, or O(1). Tail call elimination is thus required by the standard definitions of some programming languages, such as Scheme,[4][5] and languages in the ML family among others. In the case of Scheme, the language definition formalizes the intuitive notion of tail position exactly, by specifying which syntactic forms allow having results in tail context.[6] Implementations allowing an unlimited number of tail calls to be active at the same moment, thanks to tail call elimination, can also be called 'properly tail-recursive'.[4]

Besides space and execution efficiency, tail call elimination is important in the functional programming idiom known as continuation-passing style (CPS), which would otherwise quickly run out of stack space.


Related Solutions

Given the following Scheme definition: (define x '(define (fac n) (if (= n 0) 1 (*...
Given the following Scheme definition: (define x '(define (fac n) (if (= n 0) 1 (* n (fac (- n 1)))))) (This does not define the factorial function fac, but the variable x.) Write Scheme expressions in terms of x that would have the effect of extracting the following expressions: (fac n) 0 (- n 1) The second occurrence of fac. The last occurrence of n. E.g., the expression (car x) would extract define.
Define the following function f(n) = 5(2^n)-(2^(n-1)), n ≥ 1. Write a recursive definition for the...
Define the following function f(n) = 5(2^n)-(2^(n-1)), n ≥ 1. Write a recursive definition for the function f(n)? Consider the following recurrence: an= 2an-1 + 3 (where a1 = 1). Compute the values of an for n ≤ 5. Find a solution for the recurrence definition and validate its correctness. Consider the following recurrence: an=2an-1 +an-1an-2 (where a1 = 1). Compute the values of an for n ≤ 5.
Define and explain each of the following types of common accounting frauds.  Make sure to explain what...
Define and explain each of the following types of common accounting frauds.  Make sure to explain what types of accounts/transactions are considered in each fraud instance What are some companies that have done these frauds 1.Bill and Hold 2.Lapping 3. Channel Stuffing 4.Improper Assest capitlaization 5.Cookie Jar Reserves
Document where I have # to explain what the code is doing. (5 of the 20...
Document where I have # to explain what the code is doing. (5 of the 20 points will be for documenting) Fill in the __ and __________ spaces to complete the code. Logic """ This program approximates the value of pi using an algorithm designed by the German mathematician Gottfried Leibniz. The algorithm is as follows: pi = 4 - 4 / 3 + 4 / 5 - 4 / 7 + . . . This program allows the user...
Explain the following code using comments next to the code: void foo() { uint8_t a=2; uint8_t...
Explain the following code using comments next to the code: void foo() { uint8_t a=2; uint8_t b[]={b0, b1, b2}; // They are the last three digits of your A# uint8_t* c=b; uint8_t* d=&a;
In an attempt to explain what an elder matrix is: it is a n by m...
In an attempt to explain what an elder matrix is: it is a n by m matrix in which each row and each column is sorted in ascending order. Inputs in the matrix can either be finite integers or ∞. the ∞symbol is used when accounting for nonexistent inputs. for all questions below please answer using pseudocode and explainations (a) create an algorithm EXTRACT-MIN on an Elder Matrix that is not empty. the algorithm must run in O(m+n) time. The...
1. Explain M&M proposition 1 2. What is Profitability Index and its pros and cons? 3....
1. Explain M&M proposition 1 2. What is Profitability Index and its pros and cons? 3. What is Payback Period? And its pros and cons? 4. What is NPV? And is it pros and cons? 5. Define financial leverage and explain what an unlevered company means.
n this paper, please discuss the following case study. In doing so, explain your approach to...
n this paper, please discuss the following case study. In doing so, explain your approach to the problem, support your approach with references, and execute your approach. Provide an answer to the case study’s question with a recommendation. You are the owner of a parasailing company that is expanding operations to a new beachfront location, and you need to prepare a three-year analysis for the bank that may loan you the funds to purchase your boat and parasailing equipment. Because...
n following code if variable n=0, a=5 and b=10 then what will be the value of...
n following code if variable n=0, a=5 and b=10 then what will be the value of variable ‘n’ after while loop executed. Show your working as well. while(n<=(a^b)) { n++; } printf("%d",n)
Consider the following pseudo-code: /* Global memory area accessible by threads */ #define N 100 struct...
Consider the following pseudo-code: /* Global memory area accessible by threads */ #define N 100 struct frame *emptyStack[N]; struct frame *displayQueue[N]; int main() { /* ** Initialise by allocating the memory for N frames ** And place the N frame addresses into the ** empty Stack array */ Initialise(); thread_t tid1, tid2; threadCreate(&tid1, GetFrame); threadCreate(&tid2, ShowFrame); sleep(300); } GetFrame() { struct frame *frame; struct frame local; while (1) { CameraGrab(&local); /* get a frame from the camera store it in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT