**ECS 170: Introduction to AI**

**Homework Assignment # 0**

**Due: 14 January 2003**

(100 points)

**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?

ATOM

17

(0)

NIL

(A.B)

2. (3 points) Convert the following S-expressions into dot notation

(A)

((B))

( ( ) ( ) )

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.

;;; EXAMPLE1A.LSP

(defun EXAMPLE1 ( )

(let ((x nil) )

(loop

(cond

(( 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)`

` (- x)`

` x))`

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)`

` (t else-clause)))`

The
following examples show `new-if`
working as intended.

? (new-if nil 'then 'else)

ELSE

? (new-if t 'then 'else)

THEN

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 functionmapcartakes 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, callednew-mapcar, that produces the same result without using

any of the built-in mapping functions.

Department
of Computer Science

University of California at Davis

Davis, CA 95616-8562