Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Trying to make some sense of these HW questions... (Here is what I have so far)

16 views
Skip to first unread message

alphabe...@live.com

unread,
Feb 11, 2009, 1:45:13 PM2/11/09
to
This is way over my head. Help?

What are the answers to these questions and why?

Praying someone out there likes me.

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:
________________

MY WORK:


1. Life
its‐tough

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.

MY WORK:


CL-USER 1 > (defun who-knows (lst1 lst2)
(cond ((= (length lst1) (length lst2))
(+ (length lst1) (length lst2)))
((> (length lst1) (length lst2)) (length lst2))
(t (length lst1))))
WHO-KNOWS

CL-USER 2 > (blength '(a b c d))

Error: Undefined operator BLENGTH in form (BLENGTH (QUOTE (A B C D))).
1 (continue) Try invoking BLENGTH again.
2 Return some values from the form (BLENGTH (QUOTE (A B C D))).
3 Try invoking something other than BLENGTH with the same arguments.
4 Set the symbol-function of BLENGTH to another function.
5 Set the macro-function of BLENGTH to another function.
6 (abort) Return to level 0.
7 Return to top loop level 0.

Type :b for backtrace, :c <option number> to proceed, or :? for other
options

CL-USER 3 : 1 > (defun who-knows (lst1 lst2)
(cond ((= (length lst1) (length lst2))
(+ (length lst1) (length lst2)))
((> (length lst1) (length lst2)) (length lst2))
(t (length lst1))))
WHO-KNOWS

CL-USER 4 : 1 >

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)

Tamas K Papp

unread,
Feb 11, 2009, 2:41:58 PM2/11/09
to
On Wed, 11 Feb 2009 10:45:13 -0800, alphabet_rules wrote:

> This is way over my head. Help?

If this is way over your head, just drop the course.

> What are the answers to these questions and why?
>
> Praying someone out there likes me.

Can't say that I do. It is fine to ask specific questions, but
posting your HW problems and expecting a solution reveals that you are
an idiot.

Tamas

cartercc

unread,
Feb 11, 2009, 3:05:54 PM2/11/09
to
On Feb 11, 1:45 pm, alphabet_ru...@live.com wrote:
> This is way over my head. Help?
>
> What are the answers to these questions and why?

I'm in the same boat you are, and I have a suggestion. Write the
answers, taking as much time as you need to. If you get it, fine. If
you can't get it, post the question, your attempted answer, the
debugging information if appropriate, and perhaps someone will help
you with a pertinent tip.

The only way you learn is by falling down. You never learn anything by
doing it right. It's by writing wrong answers that you learn to write
the right answer.

CC

alphabe...@live.com

unread,
Feb 11, 2009, 3:46:41 PM2/11/09
to

Thanks for not being a jerk like the anal young man above me.

I posted how far I've gotten so far, but like I said I don't do
programming (I'm a business major and need this last class to
graduate).

Zach Beane

unread,
Feb 11, 2009, 3:48:16 PM2/11/09
to
cartercc <cart...@gmail.com> writes:

[snip]


> The only way you learn is by falling down. You never learn anything by
> doing it right. It's by writing wrong answers that you learn to write
> the right answer.

I strongly disagree. I'm reminded of a message from this newsgroup from
quite a while ago:

If you only sit down to toy with a language until you "get it", and
refuse to study it seriously, including reading specifications and
other people's code, you end up writing code like some people write
SMTP or NNTP servers and mail and news software in general -- and your
code will rely on the ability of others to be liberal in what they
accept. It is a really bad idea to believe that one can learn to get
it right from doing it wrong many times.

http://groups.google.com/group/comp.lang.lisp/msg/1a74ecdee0732911 has
the whole message.

Zach

Message has been deleted

Alexander Lehmann

unread,
Feb 11, 2009, 4:09:21 PM2/11/09
to
alphabe...@live.com wrote:
> Here is where I'm at after an hour:
> 3. (I don't understand)

You're supposed to define a function that takes one argument and depending on
the type of the argument does the following:

- If the argument is a list (hint: listp), return its length (hint: length).
- If the argument is something else, print an error message that not only
prints the argument's value, but also its type (hint: type-of).

Hope that helps a little.

cartercc

unread,
Feb 11, 2009, 4:12:02 PM2/11/09
to
On Feb 11, 3:48 pm, Zach Beane <x...@xach.com> wrote:
> > The only way you learn is by falling down. You never learn anything by
> > doing it right. It's by writing wrong answers that you learn to write
> > the right answer.
>
> I strongly disagree. I'm reminded of a message from this newsgroup from
> quite a while ago:

I know you were a child once, but if you are like me the experiences
of childhood have (mercifully) been forgotten. If you have children,
and I'm working on two children and two grandchildren, you will
understand the truth of that I have written.

You don't learn by doing it right. I've coached baseball, taught
music, taught programming, and been involved in many other learning
activities, including a couple of professional careers and a couples
of businesses. You learn from your mistakes, not your successes. This
is a Life Rule. If you do it right the first time, you don't know what
can go wrong, and knowing what NOT to do is the basis of real success.

CC

alphabe...@live.com

unread,
Feb 11, 2009, 4:52:49 PM2/11/09
to
On Feb 11, 3:09 pm, Alexander Lehmann <lehma...@in.tum.de> wrote:


CL-USER 1 > (defun <4> <parameter-list> <body>)
<4>

CL-USER 2 > (blength '(a b c d))

Error: Undefined operator BLENGTH in form (BLENGTH (QUOTE (A B C D))).
1 (continue) Try invoking BLENGTH again.
2 Return some values from the form (BLENGTH (QUOTE (A B C D))).
3 Try invoking something other than BLENGTH with the same arguments.
4 Set the symbol-function of BLENGTH to another function.
5 Set the macro-function of BLENGTH to another function.
6 (abort) Return to level 0.
7 Return to top loop level 0.

Type :b for backtrace, :c <option number> to proceed, or :? for other
options

CL-USER 3 : 1 >
(defun <(blength '(a b c d))> <parameter‐list> <body>)

Error: Redefining function < visible from package COMMON-LISP.
1 (continue) Redefine it anyway.
2 (abort) Return to level 1.
3 Return to debug level 1.
4 Return to level 0.
5 Return to top loop level 0.

Type :b for backtrace, :c <option number> to proceed, or :? for other
options

CL-USER 4 : 2 > (defun <blength 'a b c d> <parameter-list> <body>)
<BLENGTH

CL-USER 5 : 2 >

alphabe...@live.com

unread,
Feb 11, 2009, 5:06:33 PM2/11/09
to
Hey I've got a question: Why are people in the programming community
so anti-help with homework, in general?

I've probably posted this on 6 different forums and I've either had
people delete it, ignore it or be rude about it. There have been two
guys in this topic that have been pretty helpful, but even they've
stopped short of giving the answer.

I must confess that when taking History classes/Psych classes/business
classes I've asked for answers before, but normally people are pretty
forthcoming/helpful with them. Why not in this case?

Is there something in the LISP communities history that I've missed? I
understand that I'm pretty ignorant about programming (I only touch my
computer to write papers for the most part--me being in this class is
an accident of circumstance), and so I'm not privy to internet rules/
etiquette. Just trying to see what I'm missing about the community
because I don't want to be "that guy" but at the same time if it's
just the way I asked I'd like to know in the future what I should be
doing differently.

I would imagine that there are probably people that knew the answer to
these questions as soon as they saw them. Just wondering what the
larger reason for not answering them is.

Barry Margolin

unread,
Feb 11, 2009, 6:12:54 PM2/11/09
to
In article
<fbfab64a-d6f9-4e30...@r10g2000prf.googlegroups.com>,
alphabe...@live.com wrote:

> I would imagine that there are probably people that knew the answer to
> these questions as soon as they saw them. Just wondering what the
> larger reason for not answering them is.

Because programming is an art, a process. You have to go through the
steps yourself to learn it. You don't gain anything by having someone
spoon-feed you the answers.

Could you learn to paint by asking someone to paint a portrait for you?
Can you become a carpenter by buying furniture from IKEA?

We'll help if it looks like the poster made an attempt to solve the
problem. If he shows his work, we'll point out where he went wrong, and
he'll learn. Or if he's gotten part-way through and has questions about
how to proceed, we'll answer them.

But simply posting the problem set and saying "help me" looks like he's
just trying to get us to help him cheat.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

Zach Beane

unread,
Feb 11, 2009, 7:51:04 PM2/11/09
to
cartercc <cart...@gmail.com> writes:

> On Feb 11, 3:48 pm, Zach Beane <x...@xach.com> wrote:
>> > The only way you learn is by falling down. You never learn anything by
>> > doing it right. It's by writing wrong answers that you learn to write
>> > the right answer.
>>
>> I strongly disagree. I'm reminded of a message from this newsgroup from
>> quite a while ago:
>
> I know you were a child once, but if you are like me the experiences
> of childhood have (mercifully) been forgotten. If you have children,
> and I'm working on two children and two grandchildren, you will
> understand the truth of that I have written.

You've expressed an absolute truth about /only/ learning by failing, and
/never/ learning by succeeding. I still strongly disagree. I even have
two kids!

> You don't learn by doing it right.

In that case, maybe this poster has extra special meaning for you:

http://xach.livejournal.com/170311.html

Zach

David Golden

unread,
Feb 11, 2009, 8:10:36 PM2/11/09
to
alphabe...@live.com wrote:

> I would imagine that there are probably people that knew the answer to
> these questions as soon as they saw them. Just wondering what the
> larger reason for not answering them is.

Because they're trying to help you learn, not cheat on your homework...

You've done something similar to saying "my homework is '2+3=?', here's
my work so far '2%Emonkey'". When from your "work so far" (hey at
least you gave some!) it's looking a bit doubtful you really even know
what "2", "+", "3", "=" and "?" are, telling you "5" at that point
would probably teach you nearly nothing if you're honest and just
enable cheating behaviour if you're not, you need to be told

0
1 /
2 //
3 ///
etc.
and
/ = /
1 = 1
etc.
and
/ + // = / //
1 + 2 = 3
etc...

> CL-USER 3 : 1 >
> (defun <(blength '(a b c d))> <parameter‐list> <body>)
>
> Error: Redefining function < visible from package COMMON-LISP.
> 1 (continue) Redefine it anyway.

I mean, that looks like you actually typed in
"(defun <(blength '(a b c d))> <parameter-list> <body>)"

??? So at best you're quite confused. Not going to explain in depth
(see the book I'm about to link to), but that's ...not right. Anyway,
it's clear you need to develop a sensible basic mental model of
programming and lisp programming in particular. Nothing to be ashamed
of in itself - so long as you're actually willing to learn!

At this stage you should maybe be asking a teaching assistant of your
actual class directly for some some help, but maybe you could start to
work through:

"Common Lisp: A Gentle Introduction to Symbolic Computation"
by David S. Touretzky

It's free online these days, and is oriented towards complete
programming beginners rather than the more usual these days "lisp
beginner who has at least programmed in some language before". After
the first few chapters you should be well on your way to doing your own
homework!
http://www.cs.cmu.edu/~dst/LispBook/index.html


Thomas A. Russ

unread,
Feb 11, 2009, 8:53:48 PM2/11/09
to
alphabe...@live.com writes:

> On Feb 11, 2:05滋pm, cartercc <carte...@gmail.com> wrote:
>
> I posted how far I've gotten so far, but like I said I don't do
> programming

Well, generally we like to see a little more progress before jumping
in. As far as I could tell, you didn't really show much work at all.
It seemed to be mostly posting the actual questions as provided by your
instructor. That makes it really hard to figure out what you understand
or don't understand.

So, what did you try to do?
What questions do YOU have, as opposed to your instructor.
For example, do you know the difference between local and global values?

Of course, it would also be nice if the indentation were more
conventional. It really helps to make the structure of the code
clearer.

> (I'm a business major and need this last class to
> graduate).

Indeed. Well, it looks like outsourcing isn't going so well. Perhaps
engaging the services of a consultant would be more effective.

I do wonder what sort of business course this would be where you get
lisp questions. It it some sort of survey of programming languages?
Are you intending to enter the information technology industry? Will
you be deciding what programming language is needed for software
projects?


--
Thomas A. Russ, USC/Information Sciences Institute

cartercc

unread,
Feb 11, 2009, 9:20:58 PM2/11/09
to
On Feb 11, 7:51 pm, Zach Beane <x...@xach.com> wrote:
> You've expressed an absolute truth about /only/ learning by failing, and
> /never/ learning by succeeding. I still strongly disagree. I even have
> two kids!
>
> > You don't learn by doing it right.

Maybe I should offer an apology, or an explanation. I think this is
true especially about learning to program in a new language, which was
the OP's problem.

We can read the book, type the examples, never make a mistake, and not
really learn how to do it for real. Okay, we learn a lot from reading
the book, I won't deny that. But (usually) there's only one right way
to do it, and a million ways to do it wrong. We only find out about
the wrong ways by doing it wrong. Sometimes we make the same mistake
multiple times.

How many times have you written a perfect script on the first go? A
few times, maybe, but most of the time we get it wrong many times
before we get it right. We don't learn to ride a bike without falling
down. We don't learn to play a musical instrument without hitting a
few wrong notes. We don't learn to cook by ruining some dishes. In
general, and particularly with respect to software development, we
learn by doing, and doing multiple times. It's a rarity that we get it
right the first time. This is what I meant.

CC

cartercc

unread,
Feb 11, 2009, 9:28:45 PM2/11/09
to
On Feb 11, 5:06 pm, alphabet_ru...@live.com wrote:
> Hey I've got a question: Why are people in the programming community
> so anti-help with homework, in general?

C H E A T I N G !

The only way you will learn is to attempt it yourself. If you can't
get it right after some hard tries, you will find many people on
comp.lang.etc groups very helpful, but you got to prove yourself
first.

A wise man once said that it's more important to know the right
questions than to know the right answers. The right answers won't help
you unless you know the questions they answer. With respect to
programming, the only way you will know the questions is to explore
the problems themselves.

If you get the answers without knowing the questions, it's cheating,
but it's only cheating yourself, and most people on these n.g.s have
enough integrity to refrain from assisting you from cheating yourself.
It's not sadism or malice, but it really is looking out for your best
interests.

CC

Thomas A. Russ

unread,
Feb 11, 2009, 9:04:57 PM2/11/09
to
alphabe...@live.com writes:

> Hey I've got a question: Why are people in the programming community
> so anti-help with homework, in general?

We aren't anti-help with homework. It's just that we prefer to "help
with homework" and not "do the homework for you." That is an important
distinction. So helping means explaining things that are unclear and
giving hints or nudges in the right direction, but leaving the actual
process of doing the work up to you so that you actually learn something
from this course. Given the international nature of usenet, it's not
clear where you are taking this class, but if you are paying money for
it, you should really want to get something out of it.

> I've probably posted this on 6 different forums and I've either had
> people delete it, ignore it or be rude about it. There have been two
> guys in this topic that have been pretty helpful, but even they've
> stopped short of giving the answer.
>
> I must confess that when taking History classes/Psych classes/business
> classes I've asked for answers before, but normally people are pretty
> forthcoming/helpful with them. Why not in this case?

Well, we haven't seen what sort of questions and answers you have been
seeking for those classes. But I suspect that if you were to ask for
history help like this:

1. Who was the greatest British monarch? Why?

My work so far: The greatest British monarch is ____________.

I'm not so sure if you would get an answer for the blank along with a
justification.

So, from our point of view, your initial post seems to be as if I were
to come up to you saying I'd like some help starting my own business,
but I had a few questions first:

a. What product should I sell?

b. How should I market it?

c. What do I need to do to make it successful?

What I've figured out so far:

If I'm successful, I will be rich. How do I get there?

samantha

unread,
Feb 12, 2009, 3:28:25 AM2/12/09
to
On Feb 11, 2:06 pm, alphabet_ru...@live.com wrote:
> Hey I've got a question: Why are people in the programming community
> so anti-help with homework, in general?

Why do you think you deserve the time of busy people with your
homework? Unless you seem like a reasonably bright and interesting
person to spend time helping they may not be terribly interested.
Would you take offense at that? If so then how is your taking offense
justified?


>
> I've probably posted this on 6 different forums and I've either had
> people delete it, ignore it or be rude about it. There have been two
> guys in this topic that have been pretty helpful, but even they've
> stopped short of giving the answer.


A good student does not expect to be given the answer but to be at
best given hints leading to finding the answer and how to find answers
to other questions as well.

Perhaps you should ask what kind of student you are.

Pascal J. Bourguignon

unread,
Feb 12, 2009, 8:26:30 AM2/12/09
to
cartercc <cart...@gmail.com> writes:

> On Feb 11, 5:06 pm, alphabet_ru...@live.com wrote:
>> Hey I've got a question: Why are people in the programming community
>> so anti-help with homework, in general?
>
> C H E A T I N G !
>
> The only way you will learn is to attempt it yourself. If you can't
> get it right after some hard tries, you will find many people on
> comp.lang.etc groups very helpful, but you got to prove yourself
> first.
>
> A wise man once said that it's more important to know the right
> questions than to know the right answers. The right answers won't help
> you unless you know the questions they answer. With respect to
> programming, the only way you will know the questions is to explore
> the problems themselves.

Hence the joke with:

42


> If you get the answers without knowing the questions, it's cheating,
> but it's only cheating yourself, and most people on these n.g.s have
> enough integrity to refrain from assisting you from cheating yourself.
> It's not sadism or malice, but it really is looking out for your best
> interests.
>
> CC

--
__Pascal Bourguignon__

cartercc

unread,
Feb 12, 2009, 9:28:34 AM2/12/09
to
On Feb 12, 8:26 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:

> Hence the joke with:
>
>                             42

I sometimes adjunct and occasionally (when the class will appreciate a
little humor) before a test I ask if they want the answers (assuming a
short answer test). I will then say something like this:

"There are 5 As, 6 Bs, 3 Cs, 4 Ds, 5 trues and 2 falses."

Sometimes the joke falls a little flat. :-(

CC

pls....@gmail.com

unread,
Feb 12, 2009, 1:30:37 PM2/12/09
to
On 11 Feb, 21:46, alphabet_ru...@live.com wrote:
> I posted how far I've gotten so far, but like I said I don't do
> programming (I'm a business major and need this last class to
> graduate).

The only intelligent question here seems:

Why do they *force* you to learn Lisp (one of the most powerful and
sophisticated programming systems around), if they only wanted to
teach you very basic logical tasks?

Lisp is the most high-end programming language I know of; this means
that it's impossible to learn it in 3 minutes/hours/days, because
there's too much that can go wrong for a beginner.

If your institute were smarter (and already knew Lisp), they would
have written some nice DSL (domain specific language) just for that
task.

Please blame them for asking relatively too much!
(And please don't ask here relatively too less...)

-PM

Kaz Kylheku

unread,
Feb 12, 2009, 2:08:15 PM2/12/09
to
On 2009-02-11, alphabe...@live.com <alphabe...@live.com> wrote:
> This is way over my head. Help?
>
> What are the answers to these questions and why?
>
> Praying someone out there likes me.
>
> 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)

This is undefined behavior according to the ANSI Common Lisp standard.

A global variable must be defined before a value is assigned to it.

Your prof is either an idiot, or is deliberately you a very tough trick
question.

If you have not defined the variable A, and evaluate
(setf a 'oh-boy), the result is unpredictable. It depends
on how your Lisp compiler deals with that particular undefined
behavior.

You may get an error message like ``assignment to unbound variable''.
Or the assignment may cause the variable to be defined,
(but perhaps not defined with exactly the same properties
as a properly defined variable).

> 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?

These are trick questions. The program has not defined a global variable B. It
does not exist. It does not exist before these forms are evaluated, it deos
not exist during, and it does not exist after!

The local variables only exist during the evaluation of the LIFE function. They
do not exist before or after.

And the program has not /properly/ defined a global variable A.
This may have happened as a consequence of the SETF.

Even if we were to do your homework for you, we would have to use the imaginary
Lisp language invented by your professor, and not the real ANSI Common Lisp.

If you gave a complete and correct answer for this question, including the
explanation of undefined behavior of (setf a ...), the is a chance
that your professor would probably not understand it and try to give you a
failing grade.

So you are doomed. Pay careful attention in class, take good notes, and
regurgitate everything your professor claims to be a fact. If you have
problems, go see the prof, teaching assistants, or form a study group with
other students taking that class. See whether tutors are available (tutors who
have taken that prof's class and done well, not tutors who know the real Lisp).

alphabe...@live.com

unread,
Feb 18, 2009, 3:10:34 AM2/18/09
to
Alright then, chugging away at this to try and make some sense of
things.

I've read the first 70 or so pages and I understand what a symbol and
number are now which I didn't before, and am trying to answer question
3 again.

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.

^^ That is the question and here is where I am:

My professor said:

The function blength should be able to return the proper answer
(below) depending on what is passed to the function. You can test
whether it works or not by giving '(a b c d), 'hello, 4 to the
function once it is defined. As I suggested before, you can use
'cond', 'list' , or 'length' to define blength. Thanks.

So this is what I did:

I read about cond here:

http://www.cs.ucla.edu/classes/winter04/cs161/l1/lispPrimer/node23.html

and can't figure out how to get the function to work.

My thinking was that since I can't get cond to work that if I get the
variables a b c d to be set as 4 then maybe that would work. So this
is what I did:

(setf '(a b c d))

And I got:

Error: Odd number of args to SETF: ((QUOTE (A B C D)))

So instead I did this:

(setf a 4)
4

(setf b 4)
4

(setf c 4)
4

(setf d 4)
4

(defun BLENGTH '(a b c d))

However, I get:

Error: Non-symbol (A B C D) used as variable name in function BLENGTH.

I am currently trying to use cond to define a b c d 4 as 4 seperate
symbols inside one list and also define 4 as a seperate number but am
unsure how to write it out.

I think it starts:

(if (< a b c d 4)

but am unsure of how to continue.


To further elaborate from my understanding of this error message:

Error: Non-symbol (A B C D) used as variable name in function BLENGTH.

^^ A B C D ARE symbols but LISP won't recognize them as symbols
according to the error message. I am trying to figure out how I need
to write the function to have it recognize these are symbols. I think
that if LISP realized these were symbols that maybe things would work
better.

alphabe...@live.com

unread,
Feb 18, 2009, 3:12:49 AM2/18/09
to

Pascal J. Bourguignon

unread,
Feb 18, 2009, 3:32:34 AM2/18/09
to
alphabe...@live.com writes:
> My thinking was that since I can't get cond to work that if I get the
> variables a b c d to be set as 4 then maybe that would work. So this
> is what I did:
>
> (setf '(a b c d))

Oops!


> And I got:
>
> Error: Odd number of args to SETF: ((QUOTE (A B C D)))
>
> So instead I did this:
>
> (setf a 4)
> 4

???


> (defun BLENGTH '(a b c d))

Re-Oops!


> I think it starts:
>
> (if (< a b c d 4)

???


> but am unsure of how to continue.

The problem is not in continuing. It is that you didn't learned
anything from the first lessons of Lisp your teacher gave you.

You must start over again, slowly.

Read this book, with a REPL at hand, and trying all the examples and
exercises:

GENTLE = Common Lisp: A Gentle Introduction to Symbolic Computation
http://www-cgi.cs.cmu.edu/afs/cs.cmu.edu/user/dst/www/LispBook/index.html
http://www.cs.cmu.edu/~dst/LispBook/

> To further elaborate from my understanding of this error message:
>
> Error: Non-symbol (A B C D) used as variable name in function BLENGTH.
>
> ^^ A B C D ARE symbols but LISP won't recognize them as symbols

B, A and BA are symbols. (B A BA) is a list of three symbol. This is
the B.A.BA of Lisp, the thing you learn during the first fifteen
minutes of a Lisp course. What were you doing then?

Go read the GENTLE book!

--
__Pascal Bourguignon__

Barry Margolin

unread,
Feb 18, 2009, 5:04:59 AM2/18/09
to
In article
<a3bda70e-18cc-4359...@v18g2000vbb.googlegroups.com>,
alphabe...@live.com wrote:

> My thinking was that since I can't get cond to work that if I get the
> variables a b c d to be set as 4 then maybe that would work. So this
> is what I did:
>
> (setf '(a b c d))
>
> And I got:
>
> Error: Odd number of args to SETF: ((QUOTE (A B C D)))

SETF requires a variable name and a value. You have a value there, but
no variable to assign it to.

> So instead I did this:
>
> (setf a 4)
> 4
>
> (setf b 4)
> 4
>
> (setf c 4)
> 4
>
> (setf d 4)
> 4
>
> (defun BLENGTH '(a b c d))
>
> However, I get:
>
> Error: Non-symbol (A B C D) used as variable name in function BLENGTH.

DEFUN is for defining functions, not assigning variables. The basic
syntax is

(defun <function-name> (<var1> <var2> ...)
<expressions>)

Can you see how what you wrote doesn't fit this pattern?

>
> I am currently trying to use cond to define a b c d 4 as 4 seperate
> symbols inside one list and also define 4 as a seperate number but am
> unsure how to write it out.
>
> I think it starts:
>
> (if (< a b c d 4)
>
> but am unsure of how to continue.

I have no idea what you're trying to do there. The thing you're
supposed to be using COND for is to determine if your argument is a list
or not. Why are you comparing anything to 4?

> To further elaborate from my understanding of this error message:
>
> Error: Non-symbol (A B C D) used as variable name in function BLENGTH.
>
> ^^ A B C D ARE symbols but LISP won't recognize them as symbols
> according to the error message. I am trying to figure out how I need
> to write the function to have it recognize these are symbols. I think
> that if LISP realized these were symbols that maybe things would work
> better.

Remember that '<something> is just a shorthand for (QUOTE <something>).
So when you wrote:

(defun blength '(a b c d))

it was read as:

(defun blength (quote (a b c d)))

Now match this against the structure of a function definition I provided
above. What matches <var1>, and what matches <var2>?

David Golden

unread,
Feb 18, 2009, 8:17:37 PM2/18/09
to
alphabe...@live.com wrote:

> http://www.cs.cmu.edu/~dst/LispBook/index.html

> I've read the first 70 or so pages and I understand what a symbol and
> number are now which I didn't before, and am trying to answer question
> 3 again.
>

... but unfortunately cond isn't introduced for over 40 more pages in
the book in question, and defun over another 10 - the idea of
functions is introduced before then, but how one writes a defun form
is another matter.

You do need to understand the earlier stuff to understand the later
stuff that builds on it - exercises are provided so you can check your
understanding (and the early exercises are pen+paper type things that
shouldn't take long)

>I think it starts:
> (if ...

Trying to skip ahead to "cond" in another lisp guide altogether is
...not a great idea?

Both the "gentle introduction" book and that "lisp primer" guide* are
clearly intended to be worked through from start, not by jumping around
(and/or to different guides altogether ???) - doing that you'll just
miss earlier bits _necessary for understanding_ the later bits.

Just because an "if" form is the first bit of lisp code on the page you
linked doesn't mean it's "cond" - the write-up was just
introducing "cond" by contrastin it with "if", "if" having just been
introduced on the preceding page...

* both do kinda propagate the technical issue Kaz kicked off about, but
Touretzky at least mentions defvar - you're better off learning from
the "gentle..." book.



Message has been deleted

alphabe...@live.com

unread,
Feb 20, 2009, 5:08:20 AM2/20/09
to
On Feb 18, 4:04 am, Barry Margolin <bar...@alum.mit.edu> wrote:
> In article
> <a3bda70e-18cc-4359-84ba-257bc2448...@v18g2000vbb.googlegroups.com>,

Now I just need to work on how to make
the sentences printed out using list by
filling in the blanks.

Someone meet me half way here. :)

(defun blength (x)

(cond ((listp x) (length x))

((numberp x) ( ))

(t ( ))))

Pascal J. Bourguignon

unread,
Feb 20, 2009, 5:31:53 AM2/20/09
to
alphabe...@live.com writes:

> On Feb 18, 4:04 am, Barry Margolin <bar...@alum.mit.edu> wrote:
>> In article
>> <a3bda70e-18cc-4359-84ba-257bc2448...@v18g2000vbb.googlegroups.com>,

>> Remember that '<something> is just a shorthand for (QUOTE <something>).  
>>
>> So when you wrote:
>>
>> (defun blength '(a b c d))
>>
>> it was read as:
>>
>> (defun blength (quote (a b c d)))
>>
>> Now match this against the structure of a function definition I provided
>> above.  What matches <var1>, and what matches <var2>?
>

> I need to work on how to make
> the sentences printed out using list.
>
> Someone meet me halfway here. :)

And that someone will be YOU!

You didn't read what Barry wrote. What would make we think that it's
worthwhile to pursue teaching you if you don't make any effort on your
part?

> (defun blength '(a b c d)
>

> (cond ((listp a b c d) (length 4))
>
> ((numberp 4) ( ))
>
> (t ( ))))


C/USER[314]>

(defun blength '(a b c d)

(cond ((listp a b c d) (length 4))

((numberp 4) ( ))

(t ( ))))

*** - FUNCTION: (A B C D) is not a symbol
Es posible continuar en los siguientes puntos:
USE-VALUE :R1 Input a value to be used instead.
ABORT :R2 Abort main loop
C/Break 1 USER[315]> :q
C/USER[316]>

Please correct this error first!

--
__Pascal Bourguignon__

Pascal J. Bourguignon

unread,
Feb 20, 2009, 5:36:39 AM2/20/09
to
alphabe...@live.com writes:
> Now I just need to work on how to make
> the sentences printed out using list by
> filling in the blanks.

You bet! We won't fill no blanks!

The first thing when you have a problem to solve is to read and
understand the problem. If you do that, you'll have 80% of the job
done usually.

Your requirements were that blength return the length of a list, or

>(blength 'hello)
(sorry hello is an atom)
>(blength 4)
(sorry 4 is a number)


What (sorry hello is an atom) is? How can you produce it?
What (sorry 4 is a number) is? How can you produce it?
Who printed them?


> Someone meet me half way here. :)
>
> (defun blength (x)
>
> (cond ((listp x) (length x))
>
> ((numberp x) ( ))
>
> (t ( ))))

--
__Pascal Bourguignon__

alphabe...@live.com

unread,
Feb 20, 2009, 5:49:54 AM2/20/09
to
On Feb 20, 4:36 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:

I understand that I need to say that 4 is a number and that hello is
an atom.

When I try writing it like this:

4 NUMBERP T

^^ I get nil though

And when I try numberp (4)

^^ It doesn't do anything at all.

alphabe...@live.com

unread,
Feb 20, 2009, 5:55:53 AM2/20/09
to

Also:

I used (setq hello) to set hello as an atom but it doesn't work.
Shouldn't it identify hello as an atom even before, and if not
shouldn't it once I input it?

Pascal J. Bourguignon

unread,
Feb 20, 2009, 6:14:56 AM2/20/09
to
alphabe...@live.com writes:

Why don't you try to answer the questions?

>> What (sorry hello is an atom) is?  How can you produce it?
>> What (sorry 4 is a number)    is?  How can you produce it?
>> Who printed them?

Really, try to answer these five questions first!

> When I try writing it like this:
>
> 4 NUMBERP T
>
> ^^ I get nil though

There's no way to get NIL typing this at a CL REPL:


C/USER[316]> 4 NUMBERP T
4
C/USER[317]>
*** - SYSTEM::READ-EVAL-PRINT: variable NUMBERP has no value


> And when I try numberp (4)
>
> ^^ It doesn't do anything at all.

Yes. NUMBERP is not a variable, so evaluating it will give an error.
4 is not the name of a function, so calling it will give an error.


C/USER[319]> numberp

*** - SYSTEM::READ-EVAL-PRINT: variable NUMBERP has no value


Es posible continuar en los siguientes puntos:

USE-VALUE :R1 Input a value to be used instead of NUMBERP.
STORE-VALUE :R2 Input a new value for NUMBERP.
ABORT :R3 Abort main loop
C/Break 1 USER[320]> :q
C/USER[321]> (4)

*** - EVAL: 4 is not a function name; try using a symbol instead


Es posible continuar en los siguientes puntos:
USE-VALUE :R1 Input a value to be used instead.
ABORT :R2 Abort main loop

C/Break 1 USER[322]> :q
C/USER[323]>


--
__Pascal Bourguignon__

Pascal J. Bourguignon

unread,
Feb 20, 2009, 6:22:32 AM2/20/09
to
alphabe...@live.com writes:
> I used (setq hello) to set hello as an atom but it doesn't work.
> Shouldn't it identify hello as an atom even before, and if not
> shouldn't it once I input it?

You haven't been reading GENTLE, have you?

You really should read very carefully this book, trying out all the
examples at your REPL, doing each exercises.


HELLO is a SYMBOL. Therefore it is not a CONS cell, therefore it is an ATOM.

There are various expressions that can produce a symbol such as HELLO,
when evaluated:

(read-from-string "HELLO")
(intern "HELLO")
(quote HELLO)

are a few examples.

What? What are YOU doing? Aren't you trying them out at your REPL?
You MUST DO THAT! RIGHT NOW!

Like this:

C/USER[323]> (read-from-string "HELLO")
HELLO ;
5
C/USER[324]> (intern "HELLO")
HELLO ;
:INTERNAL
C/USER[325]> (quote HELLO)
HELLO
C/USER[326]>

Notice how the first forms produced another value in addition to the
symbol HELLO. Additionnal values are ignored in normal expressions,
so you don't need to bother with them for now.

Now that you can produce a symbol, you can ask your REPL whether it is
an ATOM:

(atom (quote HELLO))

So, what does your REPL answer?

What does SETQ do? (search the CLHS, or read the GENTLE book)


Please answer all the questions we ask!

--
__Pascal Bourguignon__

alphabe...@live.com

unread,
Feb 20, 2009, 6:29:09 AM2/20/09
to
What I don't understand is how even though LISP already recognizes
that hello is an atom I still need to do something else.

Inputting

(atom hello)

into the primers gives back t

At first I was putting

>(setq atom hello)

But it just produced 4.

Which makes sense considering that hello is already an atom.

But if that's the case then I shouldn't be getting nil as LISP already
knows (blength 'hello) is an atom.

I'm going to try the things you posted but this is what doesn't make
sense to me.

alphabe...@live.com

unread,
Feb 20, 2009, 6:35:29 AM2/20/09
to
On Feb 20, 5:22 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:

>(setq x ’(hello))

Returns hello.

SET stores a value, but how does that help me when the hello is
already internally identified as an atom?

Pascal J. Bourguignon

unread,
Feb 20, 2009, 6:40:25 AM2/20/09
to
alphabe...@live.com writes:

> What I don't understand is how even though LISP already recognizes
> that hello is an atom I still need to do something else.
>
> Inputting
>
> (atom hello)
>
> into the primers gives back t

It's possible but this is not what I asked.

(atom hello) doesn't ask whether the symbol hello is an atom. It asks
something else.


Please answer the questions I ask!

Now that you can produce a symbol, you can ask your REPL whether it is
an ATOM:
(atom (quote HELLO))

So, what does your REPL answer?


--
__Pascal Bourguignon__

Pascal J. Bourguignon

unread,
Feb 20, 2009, 6:42:05 AM2/20/09
to
alphabe...@live.com writes:
>> What does SETQ do? (search the CLHS, or read the GENTLE book)
>>
>> Please answer all the questions we ask!
>>
>> --
>> __Pascal Bourguignon__
>
>>(setq x ’(hello))
>
> Returns hello.

This is wrong.

SETQ does return something, it's true. But what does it do in
addition to returning something?


> SET stores a value, but how does that help me when the hello is
> already internally identified as an atom?

Do we care what SET does?
I asked what does SETQ do!


--
__Pascal Bourguignon__

alphabe...@live.com

unread,
Feb 20, 2009, 6:42:06 AM2/20/09
to
On Feb 20, 5:22 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:

And what does REPL stand for? I haven't run across it in GENTLE so far
at all.

I'm guessing you might mean replace but I'm not seeing why I would
want to replace hello when I am trying to get:

>(blength 'hello)
(sorry hello is an atom)

And hello is already an atom to begin with.

alphabe...@live.com

unread,
Feb 20, 2009, 6:44:17 AM2/20/09
to

When I plug:

(atom (quote HELLO))

into LISP it responds with T

So it is saying that this is true.

alphabe...@live.com

unread,
Feb 20, 2009, 6:44:26 AM2/20/09
to
On Feb 20, 5:42 am, alphabet_ru...@live.com wrote:

When I plug:

alphabe...@live.com

unread,
Feb 20, 2009, 6:47:56 AM2/20/09
to
On Feb 20, 5:42 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:

I don't know what I am doing differently than you but when I put in:

(setq x ’(hello))

^^ I get hello.

And this is what it says setq does:

10.10 SETQ AND SET
In earlier Lisp dialects, where SETF and generalized variables were
not
available, the assignment function was called SETQ. The SETQ special
function is still around today. Its syntax is the same as the SETF
macro, and it
can be used to assign values to ordinary (but not generalized)
variables.
> (setq x ’(slings and arrows))
(SLINGS AND ARROWS)

I'm not sure exactly what it is trying to say but from my
understanding setq can assign values to variables.

alphabe...@live.com

unread,
Feb 20, 2009, 6:59:56 AM2/20/09
to
Okay I'm going to sleep now.

I'll check on this tomorrow. Hope I figure this out soon since I also
have 3 assignments to do in this program called ACT-R6 by next
Wednesday.

Hopefully compared to this it's a breeze.

Kojak

unread,
Feb 20, 2009, 7:06:01 AM2/20/09
to
Le Fri, 20 Feb 2009 03:42:06 -0800 (PST),
alphabe...@live.com a écrit :

> And what does REPL stand for? I haven't run across it in GENTLE so far
> at all.

REPL stand for (R)ead, (E)valuate, (P)rint and (L)oop

(1) Read
(what you are typing)

(2) Evaluate
(what you juste finish to type and validate)

(3) Print
(the result of what the evaluation step has done
with what you have typed)

(4) Loop
(we restart at step 1)


Good luck,

--
Jacques.

Pascal J. Bourguignon

unread,
Feb 20, 2009, 8:10:34 AM2/20/09
to
alphabe...@live.com writes:
> And what does REPL stand for? I haven't run across it in GENTLE so far
> at all.

REPL is an abreviation for Read Eval Print Loop.
This is the basic abstract interaction mechanism of lisp systems.

You can try it:

(LOOP (PRINT (EVAL (READ))))

Then it will READ an expression (type for example (+ 1 2) RET)
which it will then EVALuate, and the result of the evaluation
will be PRINTed, and it will LOOP again.

In the following, I underlined with ^^ the text I typed.

C/USER[327]> (LOOP (PRINT (EVAL (READ))))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
(+ 1 2)
^^^^^^^
3 (quote hello)
^^^^^^^^^^^^^
HELLO (atom (quote hello))
^^^^^^^^^^^^^^^^^^^^
T C-c C-c
^^^^^^^
*** - Ctrl-C: Interrupción del usuario


Es posible continuar en los siguientes puntos:

ABORT :R1 Abort main loop
C/Break 1 USER[328]> :q
^^
C/USER[329]>


Common Lisp specifies some additionnal features for the REPL, and
implementations usually add some more, but these four operators
represent the basic functionality.

The REPL is the first thing described in GENTLE after the
paper-and-pencil explanations and exercises, right at the start of the
most fundamental section, the PI section (3.14 THE READ-EVAL-PRINT LOOP).

--
__Pascal Bourguignon__

Pascal J. Bourguignon

unread,
Feb 20, 2009, 8:13:28 AM2/20/09
to
alphabe...@live.com writes:

> On Feb 20, 5:42 am, alphabet_ru...@live.com wrote:
>> On Feb 20, 5:22 am, p...@informatimago.com (Pascal J. Bourguignon)
>> wrote:
>>
>> > Like this:
>>
>> > C/USER[323]>      (read-from-string "HELLO")
>> > HELLO ;
>> > 5
>> > C/USER[324]>      (intern "HELLO")
>> > HELLO ;
>> > :INTERNAL
>> > C/USER[325]>      (quote HELLO)
>> > HELLO
>> > C/USER[326]>
>>
>> > Notice how the first forms produced another value in addition to the
>> > symbol HELLO.  Additionnal values are ignored in normal expressions,
>> > so you don't need to bother with them for now.
>>
>> > Now that you can produce a symbol, you can ask your REPL whether it is
>> > an ATOM:
>>
>> >                  (atom (quote HELLO))
>>
>> > So, what does your REPL answer?
>

> When I plug:
>
> (atom (quote HELLO))
>
> into LISP it responds with T
>
> So it is saying that this is true.

Right. That means that the symbol HELLO is an ATOM.

Now if you substitute HELLO with something else in the above
expression, you'll see whether it's also an ATOM. Try the following
expressions:

(atom (quote WORLD))
(atom (quote 42))
(atom (quote "Is it an atom?"))
(atom (quote (is it an atom?)))
(atom (quote (and . this?)))
(atom (quote nil))
(atom (quote ()))


What results do you get?
Can you explain why?

--
__Pascal Bourguignon__

Pascal J. Bourguignon

unread,
Feb 20, 2009, 8:20:34 AM2/20/09
to
alphabe...@live.com writes:

> On Feb 20, 5:42 am, p...@informatimago.com (Pascal J. Bourguignon)
> wrote:
>> alphabet_ru...@live.com writes:
>> >> What does SETQ do? (search the CLHS, or read the GENTLE book)
>>
>> >> Please answer all the questions we ask!
>>
>> >> --
>> >> __Pascal Bourguignon__
>>
>> >>(setq x ’(hello))
>>
>> > Returns hello.
>>
>> This is wrong.
>>
>> SETQ does return something, it's true.  But what does it do in
>> addition to returning something?
>>
>> > SET stores a value, but how does that help me when the hello is
>> > already internally identified as an atom?
>>
>> Do we care what SET does?
>> I asked what does SETQ do!
>
>

> I don't know what I am doing differently than you but when I put in:
>
> (setq x ’(hello))
>
> ^^ I get hello.

What you do differently, is that you are not watching closely enough.

C/USER[329]> (setq x '(hello))
(HELLO)
^^^^^^^
This is not hello

This is a LIST containing one SYMBOL named "HELLO", or for short, a
LIST containing the symbol HELLO.

And there are big differences between a LIST containing something and
a SYMBOL; try the following expressions to see one aspect of these
difference:

(atom (quote HELLO))
(atom (quote (HELLO)))

> And this is what it says setq does:
>
> 10.10 SETQ AND SET
> In earlier Lisp dialects, where SETF and generalized variables were
> not
> available, the assignment function was called SETQ. The SETQ special
> function is still around today. Its syntax is the same as the SETF
> macro, and it
> can be used to assign values to ordinary (but not generalized)
> variables.
>> (setq x ’(slings and arrows))
> (SLINGS AND ARROWS)
>
> I'm not sure exactly what it is trying to say but from my
> understanding setq can assign values to variables.

Exactly. SETQ assigns a values to variables.

assuming you have two variables named X, Y and Z:

(SETQ X 42)

will assign the value 42 to the variable X.

(SETQ X 1
Y 2
Z 3)

will assign the value 1 to the variable X
and then the value 2 to the variable Y
and then the value 3 to the variable Z.

(Accessorily, SETQ returns the last value assigned).


What will be the values of three variables after these three
assignments?

(SETQ X 1)
(SETQ Y (QUOTE X))
(SETQ Z X)


--
__Pascal Bourguignon__

Thomas A. Russ

unread,
Feb 20, 2009, 3:26:04 PM2/20/09
to
alphabe...@live.com writes:
>
> I need to work on how to make
> the sentences printed out using list.
>
> Someone meet me halfway here. :)

OK.

What happens when you type

(list 'the 'value 'of '(+ 3 2) 'is (+ 3 2))

at your top level lisp prompt? Note carefully the position of the quote
marks in form. After you do that, consider the following variation:

(let ((preposition 'of))
(list 'the 'value preposition '(+ 3 2) 'is (+ 3 2)))


--
Thomas A. Russ, USC/Information Sciences Institute

Thomas A. Russ

unread,
Feb 20, 2009, 3:32:55 PM2/20/09
to
alphabe...@live.com writes:

> And what does REPL stand for? I haven't run across it in GENTLE so far
> at all.

REPL is shorthand for the Read-Eval-Print-Loop, which is the top level
interface that you interact with in Lisp. Each time you type something
to your lisp system it goes through the following steps:

1. READs the input you typed.
2. EVALuates the form produced by the read step.
3. PRINTs the result of doing the eval step.
4. LOOPs back to step 1 to do it all again.

It is just the toplevel that you interact with.

(n.B. The name EVAL is used rather than EVALUATE, because the lisp
function for evaluating s-expressions is called EVAL).

alphabe...@live.com

unread,
Feb 20, 2009, 6:03:27 PM2/20/09
to
On Feb 20, 2:32 pm, t...@sevak.isi.edu (Thomas A. Russ) wrote:

I FIGURED IT OUT!!!!!!!!!!!!!!

I feel like such an idiot. Here is what I did:

(defun blength (x)

(cond ((listp x) (length x))

((numberp x) ( ))

(t (sorry hello is an atom) ( (sorry 4 is a
number) ))))

^^ I just inputted the sentences in. I feel like such an idiot, The
answer was right in front of me.

Okay... question 4. But before that:

Here is my work:

1. Global a:oh-boy Local a:undefined Global b:oh-boy Local b:
undefined

2. a. Add the 2 lengths together. If the length of the list is larger
than list 2 it returns to the length of the list.
b. Just supply one variable because it is looking for 2.

3. (defun blength (x)

(cond ((listp x) (length x))

((numberp x) ( ))

(t (sorry hello is an atom) ( (sorry 4 is a
number) ))))

and here are the questions:

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)

^^ I know you guys aren't going to give me the answers, but since I
can't check 1 and 2 in LISP can you tell me if they are right? :)

alphabe...@live.com

unread,
Feb 20, 2009, 6:03:37 PM2/20/09
to
On Feb 20, 2:32 pm, t...@sevak.isi.edu (Thomas A. Russ) wrote:

I FIGURED IT OUT!!!!!!!!!!!!!!

I feel like such an idiot. Here is what I did:

(defun blength (x)

(cond ((listp x) (length x))

((numberp x) ( ))

(t (sorry hello is an atom) ( (sorry 4 is a
number) ))))

^^ I just inputted the sentences in. I feel like such an idiot, The
answer was right in front of me.

Okay... question 4. But before that:

Here is my work:

1. Global a:oh-boy Local a:undefined Global b:oh-boy Local b:
undefined

2. a. Add the 2 lengths together. If the length of the list is larger
than list 2 it returns to the length of the list.
b. Just supply one variable because it is looking for 2.

3. (defun blength (x)

(cond ((listp x) (length x))

((numberp x) ( ))

(t (sorry hello is an atom) ( (sorry 4 is a
number) ))))

>(blength 'hello)
(sorry hello is an atom)

>(blength 4)
(sorry 4 is a number)

alphabe...@live.com

unread,
Feb 20, 2009, 6:11:27 PM2/20/09
to

Ugh... I still can't make it say: (sorry 4 is a
number)

I lists that statement as true as well but it isn't working.

alphabe...@live.com

unread,
Feb 20, 2009, 6:19:57 PM2/20/09
to
On Feb 20, 5:03 pm, alphabet_ru...@live.com wrote:

I'm reading the stuff you guys posted to see if it is in here.

Pascal J. Bourguignon

unread,
Feb 20, 2009, 6:34:12 PM2/20/09
to
alphabe...@live.com writes:
> I FIGURED IT OUT!!!!!!!!!!!!!!

What did you figure out?


> I feel like such an idiot. Here is what I did:
>
> (defun blength (x)
>
> (cond ((listp x) (length x))
>
> ((numberp x) ( ))
>
> (t (sorry hello is an atom) ( (sorry 4 is a
> number) ))))
>
> ^^ I just inputted the sentences in. I feel like such an idiot, The
> answer was right in front of me.

You haven't tried it, have you?

> Okay... question 4. But before that:

> 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)
>
> ^^ I know you guys aren't going to give me the answers,

It's way over your head, you are not ready to try to do it yet.
(however, here is a hint: use the function BUTLAST).


> but since I can't check 1 and 2 in LISP can you tell me if they are right? :)

> 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:
> ________________

You already got the answer to this question. There's no way to answer
to it, since it considers undefined behavior, and didn't specify a
specific implementation.


> 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.

> 2. a. Add the 2 lengths together. If the length of the list is larger


> than list 2 it returns to the length of the list.

This is wrong.


> b. Just supply one variable because it is looking for 2.

Since a conforming program running on a conformant implementation
cannot crash, one one hand, and that the function who-knows only
returns positive integers (when it returns), it never returns an
error.

But if it was meant to be asked how to make it signal an error, then
your solution may be accepted. Another one would be to pass as
argument a non-sequence, so LENGTH signals itself an error.

--
__Pascal Bourguignon__

Pascal J. Bourguignon

unread,
Feb 20, 2009, 6:37:07 PM2/20/09
to
alphabe...@live.com writes:
> Ugh... I still can't make it say: (sorry 4 is a
> number)

>(blength 'hello)
(sorry hello is an atom)
>(blength 4)
(sorry 4 is a number)

What (sorry hello is an atom) is? How can you produce it?
What (sorry 4 is a number) is? How can you produce it?
Who printed them?


ANSWER THESE QUESTIONS!!!


--
__Pascal Bourguignon__

William James

unread,
Feb 20, 2009, 7:47:38 PM2/20/09
to
alphabe...@live.com wrote:

>
> 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.

Clojure:

(defn blength [x]
(cond
(list? x) (count x)
(number? x) (cons 'sorry (cons x '(is a number)))
true (cons x '(is a mystery to me))
)
)

Kojak

unread,
Feb 21, 2009, 2:57:48 AM2/21/09
to
Le Fri, 20 Feb 2009 15:11:27 -0800 (PST),
alphabe...@live.com a écrit :

> [big snip]

Hi,

Did you know that CLL is not in write-only mode?

Many people try (with great patience) to guide you, but it seems
obvious that you don't care. Very strange behavior!

Furthermore, for the sake of clearness and conciseness, it would
be nice not to quote all posts in your replies, but just about
what you reply (when you reply).

Best regards,

--
Jacques.

William James

unread,
Feb 21, 2009, 3:53:51 PM2/21/09
to
Pascal J. Bourguignon wrote:

> > 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)

Clojure:

;; The best code never contains "if" or "cond".
(defn circulate-dir [s dir]
(let [x (concat (cons (last s) s) (take 1 s))]
(or (and (= dir :left) (drop-last 2 x)) (drop 2 x))))

user=> (circulate-dir (range 5) :left)
(4 0 1 2 3)
user=> (circulate-dir (range 5) :right)
(1 2 3 4 0)

William James

unread,
Feb 21, 2009, 6:03:46 PM2/21/09
to
William James wrote:

Ruby:

def cir_dir list, dir
:left == dir ? list[-1,1] + list[0..-2] : list[1..-1] + list[0,1]
end

André Thieme

unread,
Feb 21, 2009, 7:41:53 PM2/21/09
to
William James schrieb:

Instead of cons you could also say:
(list 'sorry x 'is 'a 'number), but you probably knew this.
Instead of the true in your cond you could also say
:else (concat [x] '[is a mystery to me])

It’s typical to put all closing parens in one line. I suggest
to only check if x is a sequence, and if it is, we count the
number of elements. Otherwise we can throw an exception:

(defn blength [x]
(if (seq? x)
(count x)
(throw (Exception. (str "Sorry, " x " is a " (class x))))))


André
--
Lisp is not dead. It’s just the URL that has changed:
http://clojure.org/

William James

unread,
Feb 22, 2009, 12:47:44 AM2/22/09
to
William James wrote:

((or (and (= dir :left) drop-last) drop) 2 x)))

Raffael Cavallaro

unread,
Feb 22, 2009, 1:25:40 AM2/22/09
to
On 2009-02-22 00:47:44 -0500, "William James" <w_a_...@yahoo.com> said:

> (defn circulate-dir [s dir]
> (let [x (concat (cons (last s) s) (take 1 s))]
> ((or (and (= dir :left) drop-last) drop) 2 x)))

the best code doesn't look like an entry in a code obfuscation contest either.

use "if" when you have two clear alternatives:

(defn circulate-dir [s dir]
(let [both (concat (cons (last s) s) (take 1 s))]
((if (= dir :right) drop drop-last) 2 both)))
--
Raffael Cavallaro, Ph.D.

alphabe...@live.com

unread,
Feb 23, 2009, 8:34:30 PM2/23/09
to
Okay 23rds done when I do this:

(defun blength (x)

(cond ((listp x) (length x))

((numberp x) ( (list x 'sorry 'x 'is 'a 'number) ))

(t (list x 'sorry 'hello 'is 'an 'atom) )))

I get:

>(blength '(a b c d))
4

and


>(blength 'hello)
(sorry hello is an atom)

but for some reason this:

>(blength 4)

returns:

Error: Illegal argument in functor position: (LIST X (QUOTE SORRY) (X)
(QUOTE IS) (QUOTE A) (QUOTE NUMBER)) in ((LIST X (QUOTE SORRY) (X)
(QUOTE IS) (QUOTE A) (QUOTE NUMBER))).
1 (continue) Evaluate (LIST X (QUOTE SORRY) (X) (QUOTE IS) (QUOTE
A) (QUOTE NUMBER)) and ignore the res

alphabe...@live.com

unread,
Feb 23, 2009, 8:38:04 PM2/23/09
to
Also I now know the answer to this:

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.

is this:

a. Add the 2 lengths together. If the length of list 1 is larger than
list 2 returns to the length of list 2. If the length of list 2 is
greater than the length of list 1 return to the length of list 1.

b. Just supply one variable because it is looking for 2.

Documenting answers as I find them so if someone next years needs the
answers they can find them online. I wish this hell on no one. :)

alphabe...@live.com

unread,
Feb 23, 2009, 8:41:31 PM2/23/09
to
Finally I'm this far on question 4:

(defun circulate-dir (lst x)
(cond (equal x 'left) ( )
(equal x 'right) ( )
(t ( ))))

^^ I just need to work out what blanks to fill in with what.

Help would be appreciated with what I doing wrong for question 3 to
get this error message:

CL-USER 14 : 6 > (blength 4)

Error: Illegal argument in functor position: (LIST X (QUOTE SORRY) (X)
(QUOTE IS) (QUOTE A) (QUOTE NUMBER)) in ((LIST X (QUOTE SORRY) (X)
(QUOTE IS) (QUOTE A) (QUOTE NUMBER))).
1 (continue) Evaluate (LIST X (QUOTE SORRY) (X) (QUOTE IS) (QUOTE

A) (QUOTE NUMBER)) and ignore the rest.

Barry Margolin

unread,
Feb 23, 2009, 10:34:51 PM2/23/09
to
In article <20090221085...@thor.janville.org>,
Kojak <nnt...@janville.Borg.invalid> wrote:

> Le Fri, 20 Feb 2009 15:11:27 -0800 (PST),
> alphabe...@live.com a écrit :
>
> > [big snip]
>
> Hi,
>
> Did you know that CLL is not in write-only mode?
>
> Many people try (with great patience) to guide you, but it seems
> obvious that you don't care. Very strange behavior!

I think he is missing too much and is too much of a beginner. He needs
someone to give him personal tutoring, and a newsgroup is not a good
mechanism for this. It's an OK way to help with small problems, but not
the way to learn all the basics from scratch.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

William James

unread,
Feb 24, 2009, 12:43:32 AM2/24/09
to
alphabe...@live.com wrote:

> Okay 23rds done when I do this:
>
> (defun blength (x)
>
> (cond ((listp x) (length x))
>
> ((numberp x) ( (list x 'sorry 'x 'is 'a 'number) ))

Too many parentheses?

((numberp x) (list x 'sorry 'x 'is 'a 'number) )

alphabe...@live.com

unread,
Feb 24, 2009, 12:51:56 AM2/24/09
to
Error: Undefined operator SORRY in form (SORRY 4 IS A NUMBER).
1 (continue) Try invoking SORRY again.
2 Return some values from the form (SORRY 4 IS A NUMBER).
3 Try invoking something other than SORRY with the same arguments.
4 Set the symbol-function of SORRY to another function.
5 Set the macro-function of SORRY to another function.
6 (abort) Return to level 0.
7 Return to top loop level 0.

Type :b for backtrace, :c <option number> to proceed, or :? for other
options

alphabe...@live.com

unread,
Feb 24, 2009, 12:52:46 AM2/24/09
to
That's not it but I know I'm inputting the right thing. I just can't
put my finger on what it is...

alphabe...@live.com

unread,
Feb 24, 2009, 1:09:32 AM2/24/09
to
OMG YES YOU'RE RIGHT! I could hug you right now if this wasn't the
internet.

(defun blength (x)

(cond ((listp x) (length x))

((numberp x) (list x 'sorry 'x 'is 'a 'number) )

(t (list x 'sorry 'hello 'is 'an 'atom) )))

^^ That's the answer.

One to go...

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)

(defun circulate-dir (lst x)


(cond (equal x 'left) ( )
(equal x 'right) ( )
(t ( ))))

I know I can do this...

Also for this:

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?

1. Global a:oh-boy Local a:undefined Global b:oh-boy Local b:
undefined

^^ This is correct, right?

alphabe...@live.com

unread,
Feb 24, 2009, 10:16:31 AM2/24/09
to
Okay, there comes a time when a gal must just cut her losses. Losing
several percentage points a day on this. I've got 75% right and might
get partial credit for the 4th question so I'm just going to turn it
in and move onto the next assignment.

Thanks everyone for your help. If someone wants to post the 4th answer
in the next hour or so that would be great but I think I've done about
all that I can do here.

Hopefully act-r is less tedious. :)

0 new messages