LISP Tutorial Help

Discussion of Common Lisp

LISP Tutorial Help

Postby plandr » 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)
plandr
 

Re: LISP Tutorial Help

Postby Harleqin » Wed Feb 11, 2009 5:00 pm

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.
"Just throw more hardware at it" is the root of all evil.
Svante
Harleqin
 
Posts: 71
Joined: Wed Dec 17, 2008 5:18 am
Location: Bonn, Germany

Re: LISP Tutorial Help

Postby Paul Donnelly » Wed Feb 11, 2009 8:34 pm

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.
Paul Donnelly
 
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

Re: LISP Tutorial Help

Postby lnostdal » Fri Feb 13, 2009 10:31 pm

lnostdal
 
Posts: 20
Joined: Thu Jul 03, 2008 2:01 pm
Location: Skien, Norway

Re: LISP Tutorial Help

Postby danb » Sat Feb 14, 2009 1:35 pm

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)
Last edited by danb on Sat Mar 07, 2009 12:05 am, edited 1 time in total.
danb
 
Posts: 35
Joined: Sat Jun 28, 2008 1:05 pm
Location: Urbana, Illinois, US

Re: LISP Tutorial Help

Postby Wodin » Thu Mar 05, 2009 1:25 pm

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." :)
Wodin
 
Posts: 56
Joined: Sun Jun 29, 2008 8:16 am

Re: LISP Tutorial Help

Postby danb » Sat Mar 07, 2009 12:06 am

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".
danb
 
Posts: 35
Joined: Sat Jun 28, 2008 1:05 pm
Location: Urbana, Illinois, US


Return to Common Lisp

Who is online

Users browsing this forum: Google [Bot] and 0 guests

cron