LISP Tutorial Help
LISP Tutorial Help
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)
Re: LISP Tutorial Help
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"?
Also, please wrap your code in code tags to preserve formatting, and replace the "‐" copy-paste artifacts with a hyphen each.
Also, please wrap your code in code tags to preserve formatting, and replace the "‐" copy-paste artifacts with a hyphen each.
"Just throw more hardware at it" is the root of all evil.
Svante
Svante
-
- Posts: 148
- Joined: Wed Jul 30, 2008 11:26 pm
Re: LISP Tutorial Help
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.
Re: LISP Tutorial Help
this guy also posted on comp.lang.lisp, fyi:
http://groups.google.com/group/comp.lan ... 2ecc19cbac or http://groups.google.com/group/comp.lan ... 397e75d170
http://groups.google.com/group/comp.lan ... c426351688
http://groups.google.com/group/comp.lan ... 2ecc19cbac or http://groups.google.com/group/comp.lan ... 397e75d170
http://groups.google.com/group/comp.lan ... c426351688
Re: LISP Tutorial Help
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.plandr wrote:What are the global and local values of a and b before, during, and after this command?Code: Select all
(defun life (a b) (cond ((null a) b) ((null b) a) (t 'its-tough))) >(setf a 'oh-boy) >(life 'gummi a)
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.
--Dan B.
Re: LISP Tutorial Help
Thinko. This should be "and if neither of them is null, then return the symbol ITS-TOUGH." :)danb wrote:and if they're both null, then return the symbol ITS-TOUGH."
Re: LISP Tutorial Help
You're right. I may have meant to say "non-null".Wodin wrote:This should be "and if neither of them is null, then return the symbol ITS-TOUGH."danb wrote:and if they're both null, then return the symbol ITS-TOUGH."
--Dan B.