Computer Science 3675
Summer 2002
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. C allows 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. What is the value of Scheme expression (car (cdr (cons 'horse (cons 'zebra ()))))?

    zebra. It takes the second member of list (horse zebra).

  6. Write a Scheme function called prefix so that (prefix x y) returns true if list x is a prefix of list y. Be careful to use Scheme syntax correctly.

       (define (prefix x y) 
          (cond
             ((null? x) #t)
             ((null? y) #f)
             ((equal? (car x) (car y)) (prefix (cdr x) (cdr y)))
             (else #f)
          )
        )
      

  7. If you perform a single beta reduction on expression (lambda x. lambda y.x x y)(lambda z.zz), what do you get?

    1. lambda y.lambda z.zz(lambda z.zz) y
    2. (lambda x.lambda y.xxy)(lambda x lambda y.xxy)
    3. lambda x.lambda y.(xxy)(xxy)
    4. lambda y.(lambda z.zz)(lambda z.zz) y

    lambda y.(lambda z.zz)(lambda z.zz) y (Note that answer (a) is wrong because standard precedence rules says that it is the same as lambda y.lambda z.(zz(lambda z.zz) y).)

  8. Explain the purpose of a dereference operation in the semantics of an imperative language.

    A dereference operation fetches the content of a memory cell from the current state of the computer.

  9. How does a denotational semantics differ from an operational semantics?

    The big difference is that a denotational semantics only tells you what the final answer is, where an operational semantics tells you about all of the steps that are made during the course of computation. A denotational semantics hides details that are visible in an operational semantics.

  10. Using Astarte syntax write a definition of a function called filter so that (filter p L) returns a list of all members x of list L such that p(x) is true. For example, if p is the predicate so that p(x) is true just when x < 100, then (filter p [3,81,200,34,999,3000,20]) = [3,81,34,20].

        Define filter by first
          case filter ? [] = []
          case filter ?p (?h::?t) = h::filter p t   when p(h)
          case filter ?p (?h::?t) = filter p t 
        %Define
      

  11. Indicate how the definition of filter might be changed to prevent filter from computing the entire list before returning part of the list, so that a function that only wants to look at part of the result of filter does not need to evaluate the entire result list.

    It suffices to make the function lazy. That way it will only evaluate as much as is examined. For example:

        Define filter by first
          Await then
            case filter ? [] = []
            case filter ?p (?h::?t) = h::filter p t   when p(h)
            case filter ?p (?h::?t) = filter p t 
          %Await
        %Define