Assignment1 - Lisp Problems

Optional!

The purpose of this assignment is to check that you understand the basics of Lisp. There is very little code to be written. Please read the submission guidelines before submitting.

1.  Write sequences of cars and cdrs to select the word "pear " from the following expressions:

(apple orange pear grapefruit)

((apple orange) (pear grapefruit))

(((apple) (orange) (pear) (grapefruit)))

((((apple) orange) pear) grapefruit)

2. Evaluate the following:

```(setf tools '(hammer screwdriver))
(cons 'pliers tools)
tools

(setf tools (cons 'pliers tools))
tools

(append '(saw wrench) tools)
tools

(setf tools (append '(saw wrench) tools))
tools

```

3. Define a function called SQR that squares a number.

4. Define a function that squares each of a list of numbers.

5. Define a function that, given a list, prints out the third element.

6. Define a function " last " which takes a list as an argument and returns the last element. Hint: Use the built-in function " reverse ".

7. Define a recursive function FACT, that gives N factorial.

8. Define a recursive function FLAT, that removes all the parentheses from a list, so an input of (((a b)) (c (d e) f)) would return (a b c d e f). This is a good function to try to write, to see if you really understand how lists work. It is a little simpler using COND, which is like a case statement, rather than IF.

9. Define a function that, given a digit from 0 to 9, returns the English word for it. Hint: the ASSOC function is useful for this.

10. Define a recursive function first-n, that returns the first N elements of a list.

EXTRA CREDIT

1. Define a function " palindrome " which takes a list as an argument and returns true if the list has the same sequence of symbols when read from right to left as when it is read from left to right, e.g.,

(palindrome '(a b c b a)) = T

(palindrome '(a b c c b a)) = T

(palindrome '(a b c b b)) = NIL

2.  Define a function called CYCLE that returns the list '(x x x x x x x x ...) which goes on forever. Note that this list contains the SAME character repeated an infinite number of  times. In other words, make it a cyclic graph by changing the pointer at the end of the list to point back to the beginning. Don't try to print this list (it takes forever). Instead, print the first N elements of it using the first-n function you wrote above.