ECS 170: Introduction to AI
Homework Assignment # 0
Due: 14 January 2003
The purpose of this assignment is to refresh your knowledge of LISP and to make sure you are familiar with the procedures to get an account number and run a program. We suggest that you get started on it right away, so that you will better appreciate the Lisp-based assignments.
1. (5 points) Which of the following are LISP atoms?
2. (3 points) Convert the following S-expressions into dot notation
( ( ) ( ) )
3. (4 points) What is the value of each of the following expressions?
(1 + (* 4 5) )
(car (quote (a list) ) )
(car '(a list) )
(cdr '(a list) )
4. (4 points) What is the result of evaluating the following?
(setq x '(eval '(setq x 'eval) ) )
5. (14 points) Copy the following LISP program and run it for different values of x.
(defun EXAMPLE1 ( )
(let ((x nil) )
(( null x) (format t "Enter Number:" ) (setq x (read) ) )
(( > x 39) (format t "Too Big:" ) (setq x nil ) )
(( > x 9) (prin1 'x ) (setq x (- x 10) ) )
(( = x 9) (prin1 'ix ) (setq x 0 ) )
(( > x 4) (prin1 'v ) (setq x (- x 5) ) )
(( = x 4) (prin1 'iv ) (setq x 0 ) )
(( > x 0) (prin1 'i ) (setq x (1- x) ) )
((zerop x ) (setq x nil ) ) (terpri) )
) ) ) )
Exercise 6: (10 points) Define a function that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.
Exercise 7: (10 points) The built-in Common Lisp function abs (absolute value) could be defined as follows:
(defun abs (x)
(if (< x 0)
Write a new version of the absolute-value function, called new-abs, that produces equivalent values but uses “or” and “and” instead of “if” or “cond.”
Exercise 8: (10 points) In the spirit of rewriting, consider the proposed new implementation of the “if” special form, in terms of “cond”.
(defun new-if (predicate then-clause else-clause)
(cond (predicate then-clause)
The following examples show new-if working as intended.
? (new-if nil 'then 'else)
? (new-if t 'then 'else)
Construct an example for which new-if does not produce the same results as if. Explain why it is not possible to define if as a function in terms of cond.
Exercise 9: (10 points) Write a function fringe that takes a list as argument and returns a list whose elements are all the atoms appearing in the original list or any of its sublists, arranged in left-to-right order. For example,
? (setf x (cons (list 1 2) (list 3 4)))
((1 2) 3 4)
? (fringe x)
(1 2 3 4)
? (fringe (list x x))
(1 2 3 4 1 2 3 4)
Exercise 10: (10 points) The Common Lisp function mapcar takes as arguments a function and a list, and returns a new list whose elements are the results of
applying the function to each element of the original list. Write your own implementation, called new-mapcar, that produces the same result without using
any of the built-in mapping functions.
of Computer Science
University of California at Davis
Davis, CA 95616-8562