next up previous
Contents Next: Programming Techniques Up: Recursion and Iteration Previous: Timing Function Calls

Exercises

1.
Redefine power so that it can handle negative integer exponents (without using LISP's own expt function).

2.
Write a function called MY-REMOVE which removes all occurrences of an element from a list. MY-REMOVE should use recursion. Here are a some examples of how your function should behave:
>(my-remove 'hello '(hello why dont you say hello))
(WHY DONT YOU SAY)
>(my-remove '(oops my) '(1 2 (oops my) 4 5))
(1 2 4 5)
3.
Write an iterative version of the above and call it REMOVE-I.

4.
LISP provides a predefined function MEMBER which behaves as follows:
>(member 3 '(1 2 3 4 5) )
(3 4 5)
>(member 'hey '(whats going on here) )
NIL
>(member 'key '(where did (i (put) the (key))) )
NIL
Write a recursive function called MY-MEMBER which checks for an atom inside nested lists. Here are examples:
>(my-member 3 '(1 2 3 4 5) )
T
>(my-member 'hey '(whats going on here) )
NIL
>(my-member 'key '(where did (i (put) the (key))) )
T
5.
A palindrome is something that reads the same backwards as forwards. The following is a definition of PALINDROMEP which checks if a list is a palindrome.
(defun palindromep (lst)
  (equal lst (reverse lst)) )
So, for example:
>(palindromep '(1 2 3 4 5 4 3 2 1))
T
>(palindromep '(a b b a))
T
>(palindromep '(1 2 3))
NIL
Write a recursive version of this function called R-PALINDROMEP without using the function reverse.

6.
A mathematical expression in LISP may look something like:
	(+ (* 1 2 pi) 3 (- 4 5))
This would be more readable (to most humans) in ``infix'' notation:
	((1 * 2 * pi) + 3 + (4 - 5))
Write a function INFIX which given LISP-like mathematical expressions, returns the infixed version.



© Colin Allen & Maneesh Dhagat
November 1999