LISP Tutorial Help
Posted: Wed Feb 11, 2009 10:59 am
This is way over my head. Help?
What are the answers to these questions and why?
I'm trying to figure out if I'm thinking about this correctly.
Exercises
1. Consider the following definition:
(defun life (a b)
(cond ((null a) b)
((null b) a)
(t 'its‐tough)))
Suppose you are running the LISP interpreter and you enter the following:
>(setf a 'oh‐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‐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‐DIR so that it can circulate lists in both
directions. Thus it should work as follows:
>(circulate‐dir '(1 2 3 4) 'left)
(4 1 2 3)
>(circulate‐dir '(1 2 3 4) 'right)
(2 3 4 1)
What are the answers to these questions and why?
I'm trying to figure out if I'm thinking about this correctly.
Exercises
1. Consider the following definition:
(defun life (a b)
(cond ((null a) b)
((null b) a)
(t 'its‐tough)))
Suppose you are running the LISP interpreter and you enter the following:
>(setf a 'oh‐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‐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‐DIR so that it can circulate lists in both
directions. Thus it should work as follows:
>(circulate‐dir '(1 2 3 4) 'left)
(4 1 2 3)
>(circulate‐dir '(1 2 3 4) 'right)
(2 3 4 1)