Computer Science 3675
Fall 2000
Solutions to practice questions for quiz 3

  1. What is the value of Scheme expression (car (cdr (cons 'horse (cons 'zebra ()))))?

    zebra

  2. Write a Scheme function called drop so that (drop n L) returns the result of removing the first n members from list L. If L has fewer than n members, then (drop n L) should return the empty list. For example, (drop 2 '(4 8 3 5 7)) = (3 5 7) and (drop 3 '(5 2)) = (). Be careful to use Scheme syntax correctly.

      (define (drop n L)
        (cond
           ((eq? n 0)  L)
           ((null? L)  ())
           (else       (drop (- n 1) (cdr L)))
        )
      )
      

  3. Unification is a form of pattern matching. Which of the following is not a characteristic of unification?
    1. Unification never changes the binding of a bound variable.
    2. Unification is symmetric; unifying A with B has exactly the same effect as unifying B with A.
    3. Unification is very slow, and is only used rarely during computations of logic programs.
    4. Unification can bind unbound variables.

    (c) In fact, unification is reasonably fast, and is used frequently (in fact, constantly) in logic programs. The speed of an interpreter for logic programs is determined largely by how fast its unification code is.

  4. In the following C function definition, write an @ at each point where the compiler will insert an implicit fetch. (That is, make all @ operations explicit. Each variable stands for the variable, not the value of the variable.)

           int   test( int   x)
           {
             int   y,   a[  20];
             y =  @ x;
             a[ @ y] =   40;
             return   @ a[ @ y];
           }
       

  5. Explain the difference between an lvalue and an rvalue. Does every expression have an lvalue?

    The lvalue of an expression is the value of the expression in a left context. The lvalue of a variable is the variable itself. The rvalue of an expression is the value of the expression in a right context. The rvalue of a variable is the content of the variable. Not every expression has an lvalue. For example, the constant 2 has no lvalue, since you are not allowed to write an assignment statement

         2 = e;
      

  6. 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.

  7. 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 _upto_ 100).
        Let y = each(0 _upto_ 100).
        {x*y - 2*x^2 + y == 10}
        Writeln[$(x,y)].
        {false}