CS 462/570: Homework #1 1. Write a predicate, CHECKP, which tests its argument to see if it is a list that has the same sequence of symbols when read from right to left as when read from left to right. For example: * (checkp '(a b c b a)) T * (checkp '(a b c b c)) NIL 2. Write an IF form which is equivalent to (MAX X Y). Assume only two arguments. 3. Write a COND form that produces the same result as (OR X Y Z). 4. Write a HOW-ALIKE procedure that compares numbers in different ways to define in what way they are similar. For example: * (how-alike 7 7) THE-SAME * (how-alike 3 5) BOTH-ODD * (how-alike -2 -3) BOTH-NEGATIVE * (how-alike 5 8) NOT-ALIKE 5. Write a procedure COUNT-ATOMS which counts the atoms in a given expression. For example: * (count-atoms '(a (b c d) (e f g h))) 8 6. Describe what the following procedure does: (defun try-me-1 (a-list) (cond ((null a-list) 1) ((atom a-list) 0) (t (max (+ (try-me-1 (first a-list)) 1) (try-me-1 (rest a-list)))))) 7. Describe what the following procedure does: (defun try-me-2 (a-list) (cond ((null a-list) nil) ((atom a-list) a-list) (t (cons (try-me-2 (first a-list)) (try-me-2 (rest a-list)))))) 8. Define the predicate PRESENTP which determines whether a given atom occurs anywhere in a list. Note that PRESENTP must differ from MEMBER in that MEMBER looks only at top-level elements of the list. For example: * (setf a-list'(a (b (c (d e f) (x y z)) ))) (a (b (c (d e f) (x y z)) )) * (presentp 'x a-list) T * (presentp 'h a-list) NIL 9. Consider the following list (4 1 2 0 5 3). It represents one state of the 5-puzzle (a simplification of the 8-puzzle -- see textbook, page 63 for a description of 8 puzzle), namely ----------------- | 4 | 1 | 2 | ----------------- | 0 | 5 | 3 | ----------------- The puzzle consists of 5 tiles, numbered from 1 to 5, and an "empty" tile (numbered 0 for convinience). By sliding tiles one at a time into the empty tile, the tiles can be redistributed. The goal is to reach the following configuration: ----------------- | 1 | 2 | 3 | ----------------- | 4 | 5 | 0 | ----------------- Moving a tile up, down, left or right can be viewed as moving the empty tile down, up, right or left, respectively. Thus, instead of moving 5 individual tiles, we can only consider the movements of the empty tile. The problem is to find the sequence of moves that takes the puzzle from its initial state to the goal state. To solve this problem, we have to write a function GET-NEW-STATES which returns all possible movements of the empty tile for each state. Example: * (get-new-states '(4 1 2 5 0 3)) ((4 1 2 5 3 0) (4 1 2 0 5 3) (4 0 2 5 1 3)) | | | move 0 move 0 move 0 up to the right to the left Write GET-NEW-STATES to implement all possible movements of the empty tile for a given state. I'll start it for you: (defun get-new-states (state) (setf new-states '()) (cond ((= 0 (first state)) (setf new-states ..........)) ((= 0 (second state)) (setf new-states .........)) ..... ((= 0 (sixth state)) (setf new-states ..........)))) 10. Write a recursive function DISTANCE between two bit vectors of the same length represented by lists of ones and zeros. For example, * (distance '(1 0 1 1) '(0 1 0 1)) 3 Discuss what would have to be done, if the vectors were of a different lenght. This is how to submit the homework on BB Learn: Copy each function definition and example call to a text editor as shown: Problem xx: Write a procedure that compares and returns the smaller of two argumnets: CG-USER(1): (defun user-defined-min (x y) (if (< x y) x y)) USER-DEFINED-MIN CG-USER(2): (user-defined-min 10 5) 5 Problem xx: Write a procedure that produces the same result as (AND X Y Z). CG-USER(3): (defun user-defined-and (x y z) (cond ((not x) nil) ((not y) nil) (t z))) USER-DEFINED-AND CG-USER(4): (user-defined-and t t nil) NIL CG-USER(5): (user-defined-and t t t) T CG-USER(6):