Computer Science 3675
Fall 2004
Solutions to Practice questions for quiz 3

  1. What is the purpose of the static link in a frame in the run-time stack?

    The static link points to the activation of the creator of the current function.  It is used to find nonlocal bindings.

  2. What is the purpose of the dynamic link in a frame in the run-time stack?

    The dynamic link points to the activation immediately beneath this activation in the stack.  It is used to restore the stack frame pointers when the current function returns.

  3. What information is stored in a function closure?

    A function closure holds an indication of how to run a function (such as a pointer to the instructions that perform the function) plus a pointer to an activation or environment where the function gets its nonlocal bindings.

  4. In C, you are not allowed to write a function inside another function. But C does allow you to treat a function as a value. Does C need to use function closures?

    No. Since a function cannot be created inside another function, it has no creator, and does not need to get nonlocal bindings from a creator. An implementation of C does not need to store static links in the run-time stack.

  5. Write a Scheme function called descending so that (descending L) is true for a list of numbers L if L is in descending order. (The Scheme function < does a less-than test, and > does a greater-than test.) List (a b c d) is in descending order if a > b > c > d. The empty list is in descending order by definition, as is a singleton list.

       (define (descending x)
         (cond 
           ((null? x) #t)
           ((null? (cdr x)) #t)
           (else (and (> (car x) (cadr x))
                      (descending (cdr x))))
         )
       )
      

  6. Are backtracking and exception handling the same thing? For example, can you use the exception handling mechanism of Java to do backtracking?

    No, they are not the same, and exception handling cannot be used as a substitute for backtracking. A backtracking control frame remains active even after the function that created it has returned. An exception-handling control frame is removed when the context that created the frame is exited.

  7. What is one important motivation for including exception handling in a programming language?

    Some acceptable answers include the following.

    1. Exception handling makes it possible to move error handling code out of the flow of the main program logic.
    2. Exception handling makes recovery from errors easier, thus making programmers more likely to put in the effort to do it.
    3. Exception handling makes it possible to recover from errors in parts of the program that do not perform any error checking, and might be written in an unreliable way.

  8. Using backtracking, write an Astarte program fragment that will print all solutions (x,y) to equation xy - 2x2 + y = 10, where x and y are both integers in the range 0,...,100. Do not use a loop or recursion. Your program fragment can fail when it is done.

        Let x = each [0,...,100].
        Let y = each [0,...,100].
        {x*y - 2*x^2 + y == 10}
        Writeln[$(x,y)].
        {false}