Homework #1: Common Lisp Review


A simple potpourri to get you back into the swing of Lisp. No need to do error checking (e.g. checking that a function that expects a 5 element list really gets it). However, as with all the assignments, be sure you test the code on enough cases (including the examples I give at the very least) to illustrate that it is working correctly. Hand in the code and output showing the test cases.

1. Obtain your Sun account, log in, and send me email (account name "hall").

2. The built-in function random will, if given an integer N as input, return a pseudorandom number between 0 and N-1 inclusive. Write a function that takes two integers as input and returns a number at random between the two integers (inclusive).
E.g. (Random-Range 13 8) returns 8, 9, 10, 11, 12, or 13 at random.
(Random-Range 5 10) returns 5, 6, 7, 8, 9, or 10.

3. Write a function that will give the sum of the squares of a list of numbers.

(Sum-of-Squares '(0 1 2 3 4)) ==> 30

4. Write a recursive First+Last function that returns a list of the first and last elements of a list. Don't use last, of course.

(First+Last '(a b c d e f)) ==> (A F)

5. Fibonacci numbers are defined as follows: the 0th is 0, the 1st is 1, and each of the rest is the sum of the previous two. I.e. the first eight are 0, 1, 1, 2, 3, 5, 8, and 13.
(A) Write a recursive function to calculate the Nth fibonacci number.

(Fibonacci 7) ==> 13
(B) Why is the naive version of this function so slow?

6. Write an iterative function to calculate Fibonacci numbers. This is to practice loop, and will be much faster.

7. Rmv. The built-in function remove removes elements from a list. Write a basic recursive replacement that removes the items from the list that are equal to the test item.

(Rmv 'a '(b a n a n a)) ==> (B N N)

Bonus. What is the time complexity (in asymptotic notation) of the recursive Fibonacci function?


"What a Loss! interjection. A remark to the effect that the situation is bad. Example: Suppose someone said, 'Fred decided to write his program in Ada instead of Lisp.' ..."
Guy L. Steele Jr. et al, The Hacker's Dictionary, Harper and Row, 1983.