Page 1 of 1

LISP Tutorial Help

PostPosted: Wed Feb 11, 2009 10:59 am
by plandr
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)

Re: LISP Tutorial Help

PostPosted: Wed Feb 11, 2009 5:00 pm
by Harleqin
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.

Re: LISP Tutorial Help

PostPosted: Wed Feb 11, 2009 8:34 pm
by Paul Donnelly
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

PostPosted: Fri Feb 13, 2009 10:31 pm
by lnostdal

Re: LISP Tutorial Help

PostPosted: Sat Feb 14, 2009 1:35 pm
by danb
plandr wrote:
Code: Select all
(defun life (a b)
  (cond ((null a) b)
        ((null b) a)
        (t 'its-tough)))
>(setf a 'oh-boy)
>(life 'gummi a)

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)

Re: LISP Tutorial Help

PostPosted: Thu Mar 05, 2009 1:25 pm
by Wodin
danb wrote:and if they're both null, then return the symbol ITS-TOUGH."

Thinko. This should be "and if neither of them is null, then return the symbol ITS-TOUGH." :)

Re: LISP Tutorial Help

PostPosted: Sat Mar 07, 2009 12:06 am
by danb
Wodin wrote:
danb wrote:and if they're both null, then return the symbol ITS-TOUGH."

This should be "and if neither of them is null, then return the symbol ITS-TOUGH." :)

You're right. I may have meant to say "non-null".