Review for Lisp Quizzes
CSU520 & CSG120 Artificial Intelligence - Spring 2006

Professor Futrelle - College of Computer and Information Science
Northeastern U., Boston, MA

To be held January 23rd and 30th 2006

They will be closed book, closed notes quizzes based on Chapter 1 of Norvig's Paradigms of Artificial Intelligence Programming: Case Studies in Common LISP. The examples below do not exhaust the functions or forms that might be quizzed on. In every case you should show the functions you design, their application, and the results of the application. The level of difficulty of the CSU520 and CSG120 quizzes will be different.

The CSG120 quiz will be Monday, January 23rd.

The CSU520 quiz has been rescheduled to Monday the 30th.

The questions on the quizzes will be quite similar to the examples below. (After all, there isn't that much material in Chapter 1.) The answers to the examples are not given; working out the answers will help prepare you for your quiz. Some of the quiz questions will have hints and suggestions similar to the ones in the examples below. For the recursive functions you should keep track of how the arguments and return values change at each re-application of the function.


Example 1.

Define a simple function of your own and write out the form being used to map your function over a list you design, as well as the the result of the mapping.


Example 2.

Write out a definition for a function that takes a function as an argument, applies it to each element of a list, and appends the results, i.e., mappend, pg. 19. Show an application of it using a simple function you define.


Example 3.

Give an example of a funcall of a simple lambda expression you define, being applied to some argument.


Example 4.

Define a function count-atoms-nums similar to count-atoms, pg. 32, that keeps separate counts of the numbers and non-numbers in a list, assuming no nested lists. First, define pair-sum so that (pair-sum '(3 4) '(10 20)) returns (13 24). Then define count-atoms-nums such that for each cond clause case it returns a list of two integers, the first is the count for atoms and the second for numbers, using numberp as the predicate. You have to be careful, since an integer is also an atom, so you should simultaneously test for atom and not numberp. For the recursive call, you'll need to use pair-sum to combine the results of the two calls to count-atoms-nums.
Results should look like: (count-atoms-nums '(a b c d e 1 2 3)) => (5 3)


Example 5.

Use setf to define three symbols whose values are three different lists. Show the forms you would evaluate to demonstrate that the result of appending these lists together produces copies of the first two lists. (You'll want to use eq for your tests.) You can use single item lists to make your analysis simpler. Explain the side effects that would occur if append spliced the lists together.


Example 6.

Define a recursive function nonums that generates a new list from a given one, omitting any numbers in the original list. Use the numberp predicate. You can think this through using pictures of the list structure. (A nil consed to the front of a list produces a nil in the list, so you need to have a clause that simply skips numbers and works on the rest of the list.)


Example 7.

Explain the syntax of the Lisp if form with an example. What is its major limitation?


Example 8.

Create two examples, one using apply and the other using funcall, and explain how they differ. For each example write a version of the same function application that doesn't use apply or funcall. (apply and funcall are typically used when a function is passed as an argument at run time, so you can't write such an equivalent form in advance.) This example question doesn't ask you to pass in a function to use.) Another question might ask you to explain the error in the form (apply #'first '(a b c)).


Example 9.

The "Manhattan" or "city-block" metric is a convenient measure of the distance between two points, typically in two dimensions. It is the total distance traversed by moves along one axis at a time. A list of such moves could be written as ((N 3) (E 4) (N 2)), where 'N' and 'E' abbreviate "North" and "East" respectively. The total distance for the example is 3 + 4 + 2 = 9. Define a function called manhattan, which, when give such a list, returns the appropriate sum. This can be done non-recursively by using let, dotimes, incf, and elt, as in the second example on pg. 33.


Example 10.

Explain how the second argument, depth, in following example from pg. 31, operates as a local variable that is initialized only once. Work through a simple example of its use, for example, with an argument '(a (b) c).

(defun atomprint (exp &optional (depth 0))
  "Print each atom in exp, along with its depth of nesting."
  (if (atom exp)
      (format t "~&ATOM: ~a, DEPTH ~d" exp depth)
      (dolist (element exp)
        (atomprint element (+ depth 1)))))


Return to CSU520 homepage

or return to CSG120 homepage.

or RPF's Teaching Gateway or homepage