Exercises
1. Consider the following definition:
(defun life (a b)
(cond ((null a) b)
((null b) a)
(t 'its&#8208;tough)))
Suppose you are running the LISP interpreter and you enter the following:
>(setf a 'oh&#8208;boy)
Then you do the following:
>(life 'gummi a)
What are the global and local values of a and b before, during, and after this command?
Global a: ______ Local a: __________ Global b:___________ Local b: ________________

2. Consider the following function definition:
(defun who&#8208;knows (lst1 lst2)
(cond ((= (length lst1) (length lst2))
(+ (length lst1) (length lst2)))
((> (length lst1) (length lst2)) (length lst2))
(t (length lst1))))
a. What does this function do? Be precise as what would happen in each case.
b. How would you make this function crash (return an ERROR)? Be careful in
explaining why it will happen.

3. Write a function called BLENGTH that works as follows:
>(blength '(a b c d))
4
>(blength 'hello)
(sorry hello is an atom)
>(blength 4)
(sorry 4 is a number)
Thus, if a list is passed in it should return the proper length, else if a number, or another
type of atom is passed in, it should identify them as such.

4. Consider the following definition for the function CIRCULATE:
(defun circulate (lst)
(append (rest lst)
(list (first lst))))
This function takes a list and constructs a new list by taking the first element of the old
list and making it the last element of the new. For example:
>(circulate '((whats) happening here))
(happening here (whats))
Rewrite the function and call it CIRCULATE&#8208;DIR so that it can circulate lists in both
directions. Thus it should work as follows:
>(circulate&#8208;dir '(1 2 3 4) 'left)
(4 1 2 3)
>(circulate&#8208;dir '(1 2 3 4) 'right)
(2 3 4 1)

If this is from a tutorial, you should have enough information from the preceding chapter(s) to at least post specific questions about the exercises. What is your specific problem in each case? What are your partial "thunks"?

Yes, please both fix your formatting and post your reasoning so far for each of these. It's easy to read an explanation and think you understand it fully, so I'd rather not just put the whole thing out there.

What are the global and local values of a and b before, during, and after this command?

plandr, I can help you with your homework if you need a tutor. I won't do your homework for you, but I can make it easier. For example, the COND form in your LIFE function is like a sequence of "if" and "else" statements. "(cond ((null a) b) ((null b) a) (t 'its-tough)))" is equivalent to "if a is null, then return b; otherwise, if b is null, then return a; and if they're both non-null*, then return the symbol ITS-TOUGH." You should be able to look at the values of A and B to figure out what happens in the function.

You can contact me by email if you want some help. My first name is Dan, my last name is Bensen, and my email address is my full name at att.net. Got it? We can work over telephone or email, whichever works best for you.

* (edited per Wodin's correction)

Last edited by danb on Sat Mar 07, 2009 12:05 am, edited 1 time in total.