From: doug
Subject: newbie question on CAR and CDR in CL
Date: 
Message-ID: <1147046020.794672.293650@g10g2000cwb.googlegroups.com>
Hi,

I am reading a CL tutorial book which has this exercise question:

"Using just CAR and CDR, is it possible to write a function that
returns the last element of a list,
no matter how long the list is? Explain."

Myy intuition said yes but the answer in the book says NO because:

"it is not possible to write a function to extract the last element of
a list of unknown length using
just CAR and CDR, because we do not know how many CDRs to use".

My question is why do we have to know the length of the list in advance
in this case?  Isn't the last
element in a list the only cons cell that would return T on CAR and NIL
on CDR so a recursive
application of these two functions on an arbitrary list is possible?

doug

From: justinhj
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <1147049759.883856.124850@j33g2000cwa.googlegroups.com>
doug wrote:
> "Using just CAR and CDR, is it possible to write a function that
> returns the last element of a list,
> no matter how long the list is? Explain."

The answer lies in the fact that each cons cell forms part of a singly
linked list, with each cell containing a data item and a link to the
next cell.

So without following the link to the next cell you don't know that it
is the last one until it is to late and you are at the last link with
no record of the one before it.

You would need some kind of conditional ('cond' or 'if' perhaps) at
least, so you can check the cell before you follow it.


Justin
From: Jason
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <1147056879.892988.135520@y43g2000cwc.googlegroups.com>
justinhj wrote:
> doug wrote:
> > "Using just CAR and CDR, is it possible to write a function that
> > returns the last element of a list,
> > no matter how long the list is? Explain."
>
> The answer lies in the fact that each cons cell forms part of a singly
> linked list, with each cell containing a data item and a link to the
> next cell.
>
> So without following the link to the next cell you don't know that it
> is the last one until it is to late and you are at the last link with
> no record of the one before it.
>
> You would need some kind of conditional ('cond' or 'if' perhaps) at
> least, so you can check the cell before you follow it.
>
>
> Justin

I just hacked this up (in emacs lisp), and it seems to function exactly
as the op suggested. Perhaps the book is indeed wrong?

(defun last (l)
  (if (and (car l) (cdr l))
      (last (cdr l))
    (car l)))

(setq l '(this is a test))
(setq l2 (quote (this is another test, dude!)))
(last l) ;; returns test
(last l2) ;; returns dude!

-Jason
From: Bill Atkins
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <873bfljkbh.fsf@rpi.edu>
"Jason" <·······@gmail.com> writes:

> justinhj wrote:
>> doug wrote:
>> > "Using just CAR and CDR, is it possible to write a function that
>> > returns the last element of a list,
>> > no matter how long the list is? Explain."
>>
>> The answer lies in the fact that each cons cell forms part of a singly
>> linked list, with each cell containing a data item and a link to the
>> next cell.
>>
>> So without following the link to the next cell you don't know that it
>> is the last one until it is to late and you are at the last link with
>> no record of the one before it.
>>
>> You would need some kind of conditional ('cond' or 'if' perhaps) at
>> least, so you can check the cell before you follow it.
>>
>>
>> Justin
>
> I just hacked this up (in emacs lisp), and it seems to function exactly
> as the op suggested. Perhaps the book is indeed wrong?
>
> (defun last (l)
>   (if (and (car l) (cdr l))
>       (last (cdr l))
>     (car l)))
>
> (setq l '(this is a test))
> (setq l2 (quote (this is another test, dude!)))
> (last l) ;; returns test
> (last l2) ;; returns dude!
>
> -Jason
>

You're using a conditional.

-- 
This is a song that took me ten years to live and two years to write.
 - Bob Dylan
From: R. Mattes
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <pan.2006.05.08.11.34.42.777040@mh-freiburg.de>
On Sun, 07 May 2006 19:54:39 -0700, Jason wrote:

> 
> justinhj wrote:
>> doug wrote:
>> > "Using just CAR and CDR, is it possible to write a function that
>> > returns the last element of a list,
>> > no matter how long the list is? Explain."
>>
>> The answer lies in the fact that each cons cell forms part of a singly
>> linked list, with each cell containing a data item and a link to the
>> next cell.
>>
>> So without following the link to the next cell you don't know that it
>> is the last one until it is to late and you are at the last link with
>> no record of the one before it.
>>
>> You would need some kind of conditional ('cond' or 'if' perhaps) at
>> least, so you can check the cell before you follow it.
>>
>>
>> Justin
> 
> I just hacked this up (in emacs lisp), and it seems to function exactly
> as the op suggested. Perhaps the book is indeed wrong?
> 
> (defun last (l)
>   (if (and (car l) (cdr l))
>       (last (cdr l))
>     (car l)))
> 
> (setq l '(this is a test))
> (setq l2 (quote (this is another test, dude!)))
> (last l) ;; returns test
> (last l2) ;; returns dude!
> 

Now, try:

 (setq l '(NIL 4 5  6))


Autsch!

 Cheers Ralf Mattes
> -Jason
From: Jason
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <1147131088.885366.294890@y43g2000cwc.googlegroups.com>
R. Mattes wrote:
> On Sun, 07 May 2006 19:54:39 -0700, Jason wrote:
>
> >
> > justinhj wrote:
> >> doug wrote:
> >> > "Using just CAR and CDR, is it possible to write a function that
> >> > returns the last element of a list,
> >> > no matter how long the list is? Explain."
> >>
> >> The answer lies in the fact that each cons cell forms part of a singly
> >> linked list, with each cell containing a data item and a link to the
> >> next cell.
> >>
> >> So without following the link to the next cell you don't know that it
> >> is the last one until it is to late and you are at the last link with
> >> no record of the one before it.
> >>
> >> You would need some kind of conditional ('cond' or 'if' perhaps) at
> >> least, so you can check the cell before you follow it.
> >>
> >>
> >> Justin
> >
> > I just hacked this up (in emacs lisp), and it seems to function exactly
> > as the op suggested. Perhaps the book is indeed wrong?
> >
> > (defun last (l)
> >   (if (and (car l) (cdr l))
> >       (last (cdr l))
> >     (car l)))
> >
> > (setq l '(this is a test))
> > (setq l2 (quote (this is another test, dude!)))
> > (last l) ;; returns test
> > (last l2) ;; returns dude!
> >
>
> Now, try:
>
>  (setq l '(NIL 4 5  6))
>
>
> Autsch!
>
>  Cheers Ralf Mattes

Yikes! Pount taken. When I tested your example, I actually got 6 as the
answer! However, when the tested the following, I did expose the flaw
you mention:

(defun last (l)
  (if (and (car l) (cdr l))
      (last (cdr l))
    (car l)))

(last (cons 4 (cons 5 (cons 6 (quote ()))))) ;; <<< RETURNS 6
(last (cons (quote()) (cons 4 (cons 5 (cons 6 (quote ())))))) ;; <<<
RETURNS NIL

-Jason
From: Jason
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <1147057315.726530.93260@i39g2000cwa.googlegroups.com>
doug wrote:
> Hi,
>
> I am reading a CL tutorial book which has this exercise question:
>
> "Using just CAR and CDR, is it possible to write a function that
> returns the last element of a list,
> no matter how long the list is? Explain."
>
> Myy intuition said yes but the answer in the book says NO because:
>
> "it is not possible to write a function to extract the last element of
> a list of unknown length using
> just CAR and CDR, because we do not know how many CDRs to use".
>
> My question is why do we have to know the length of the list in advance
> in this case?  Isn't the last
> element in a list the only cons cell that would return T on CAR and NIL
> on CDR so a recursive
> application of these two functions on an arbitrary list is possible?
>
> doug

It occurs to me that maybe AND and OR are not supposed to be used for
this exercise. If that is the case then the book is correct. However,
if you are allowed AND, then... well, see my other post. :)

-Jason
From: Barry Margolin
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <barmar-5BCC93.23320407052006@comcast.dca.giganews.com>
In article <·······················@i39g2000cwa.googlegroups.com>,
 "Jason" <·······@gmail.com> wrote:

> doug wrote:
> > Hi,
> >
> > I am reading a CL tutorial book which has this exercise question:
> >
> > "Using just CAR and CDR, is it possible to write a function that
> > returns the last element of a list,
> > no matter how long the list is? Explain."
> >
> > Myy intuition said yes but the answer in the book says NO because:
> >
> > "it is not possible to write a function to extract the last element of
> > a list of unknown length using
> > just CAR and CDR, because we do not know how many CDRs to use".
> >
> > My question is why do we have to know the length of the list in advance
> > in this case?  Isn't the last
> > element in a list the only cons cell that would return T on CAR and NIL
> > on CDR so a recursive
> > application of these two functions on an arbitrary list is possible?
> >
> > doug
> 
> It occurs to me that maybe AND and OR are not supposed to be used for
> this exercise. If that is the case then the book is correct. However,
> if you are allowed AND, then... well, see my other post. :)

From the answer given in the book it sounds like you're not even allowed 
to use IF.  I'm guessing that the book is trying to explain *why* you 
need conditionals -- so that you can code a function like LAST that has 
to make a decision rather than just calculating something 
straightforwardly.

-- 
Barry Margolin, ······@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 ***
From: Deon Garrett
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <mCz7g.37149$Kn4.12826@bignews2.bellsouth.net>
"Barry Margolin" <······@alum.mit.edu> wrote in message 
·································@comcast.dca.giganews.com...
> In article <·······················@i39g2000cwa.googlegroups.com>,
> "Jason" <·······@gmail.com> wrote:
>
> From the answer given in the book it sounds like you're not even allowed
> to use IF.  I'm guessing that the book is trying to explain *why* you
> need conditionals -- so that you can code a function like LAST that has
> to make a decision rather than just calculating something
> straightforwardly.

Seems like a weird example to illustrate conditions.  I wonder if the 
"answer"
wasn't instead, "No, because you haven't seen recursion yet."

Either way, not a very well-formed question.  And he had to use defun, too.

:)

-- 
jd 
From: Barry Margolin
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <barmar-29CE98.20570808052006@comcast.dca.giganews.com>
In article <·····················@bignews2.bellsouth.net>,
 "Deon Garrett" <·····@acm.org> wrote:

> "Barry Margolin" <······@alum.mit.edu> wrote in message 
> ·································@comcast.dca.giganews.com...
> > In article <·······················@i39g2000cwa.googlegroups.com>,
> > "Jason" <·······@gmail.com> wrote:
> >
> > From the answer given in the book it sounds like you're not even allowed
> > to use IF.  I'm guessing that the book is trying to explain *why* you
> > need conditionals -- so that you can code a function like LAST that has
> > to make a decision rather than just calculating something
> > straightforwardly.
> 
> Seems like a weird example to illustrate conditions.  I wonder if the 
> "answer"
> wasn't instead, "No, because you haven't seen recursion yet."
> 
> Either way, not a very well-formed question.  And he had to use defun, too.
> 
> :)

I haven't seen the book, so here's my thought about what it's trying to 
do.

They start off by teaching a few basic functions, like CAR/CDR/CONS and 
arithmetic.  Then they ask "Given what we've taught, can you implement 
LAST?"  The answer, of course, is "no".  Then it goes on to explain 
conditionals and recursion, which solve the problem.

In a similar vein, they might first show how to use arithmetic to 
calculate things like quadratic equations, and then ask if you can 
calculate factorial with what they've shown.

-- 
Barry Margolin, ······@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 ***
From: Pascal Bourguignon
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <87k68wtzls.fsf@thalassa.informatimago.com>
"doug" <·········@yahoo.com> writes:
> I am reading a CL tutorial book which has this exercise question:
>
> "Using just CAR and CDR, is it possible to write a function that
> returns the last element of a list,
> no matter how long the list is? Explain."
>
> My intuition said yes but the answer in the book says NO because:
>
> "it is not possible to write a function to extract the last element
> of a list of unknown length using just CAR and CDR, because we do
> not know how many CDRs to use".
>
> My question is why do we have to know the length of the list in
> advance in this case?  Isn't the last element in a list the only
> cons cell that would return T on CAR and NIL on CDR so a recursive
> application of these two functions on an arbitrary list is possible?

Unfortunately, often the pedagogical questions get their meaning from
the answers provided by the teachers.  You must know the expected
answers to be able to answer correctly to the question.  It helps
having a teacher parents...


From the answer, we may deduce that they expected a form like:

      (car (cdr (cdr (cdr (cdr (cdr list))))))

- this is not a function, this is just a form (an expression).
- it uses car, cdr and functional application.
- it doesn't define a function.

Moreover, I'd like to note that a "list" may not have a "last element".
A "proper-list" is defiend to have a "last element".

But there are also: "dotted-list", whose final cons is of the form (x
. y) with y not NIL, and "circular-list", that has no final cons and
no final element.  (We could define a final cons and element for
circular lists as the last cons to be walked once, that is, the cons
whose cdr refers back a cons behind it).


Finally, even if you add IF and NULL, and restrict yourself to
proper-lists, you won't be able to write a form: you need at least
also LAMBDA (and write an Y combinator, or DEFUN or LABELS, and use
the defined function recursively, or use one of the looping operators.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

CAUTION: The mass of this product contains the energy equivalent of
85 million tons of TNT per net ounce of weight.
From: Dave Seaman
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <e3nkto$5ca$2@mailhub227.itcs.purdue.edu>
On Mon, 08 May 2006 15:28:31 +0200, Pascal Bourguignon wrote:
> "doug" <·········@yahoo.com> writes:
>> I am reading a CL tutorial book which has this exercise question:

>> "Using just CAR and CDR, is it possible to write a function that
>> returns the last element of a list,
>> no matter how long the list is? Explain."

>> My intuition said yes but the answer in the book says NO because:

>> "it is not possible to write a function to extract the last element
>> of a list of unknown length using just CAR and CDR, because we do
>> not know how many CDRs to use".

>> My question is why do we have to know the length of the list in
>> advance in this case?  Isn't the last element in a list the only
>> cons cell that would return T on CAR and NIL on CDR so a recursive
>> application of these two functions on an arbitrary list is possible?

> Unfortunately, often the pedagogical questions get their meaning from
> the answers provided by the teachers.  You must know the expected
> answers to be able to answer correctly to the question.  It helps
> having a teacher parents...


> From the answer, we may deduce that they expected a form like:

>       (car (cdr (cdr (cdr (cdr (cdr list))))))

> - this is not a function, this is just a form (an expression).
> - it uses car, cdr and functional application.
> - it doesn't define a function.

That's not the conclusion I drew from the answer.  I thought the question
was asking for a function definition whose BODY uses only CAR and CDR,
and no other functions.  Therefore, DEFUN is allowed (because it doesn't
appear in the function body), but not conditionals such as IF.

If DEFUN were not allowed, then surely the answer would be much simpler
and more general than the one that was actually given.

> Moreover, I'd like to note that a "list" may not have a "last element".
> A "proper-list" is defiend to have a "last element".

I think the question used "list" and not "s-expression" precisely because
they had proper lists in mind.



-- 
Dave Seaman
U.S. Court of Appeals to review three issues 
concerning case of Mumia Abu-Jamal.
<http://www.mumia2000.org/>
From: Tayssir John Gabbour
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <1147104845.430922.220620@j33g2000cwa.googlegroups.com>
Dave Seaman wrote:
> That's not the conclusion I drew from the answer.  I thought the question
> was asking for a function definition whose BODY uses only CAR and CDR,
> and no other functions.  Therefore, DEFUN is allowed (because it doesn't
> appear in the function body), but not conditionals such as IF.

I remember disliking these questions. It was:

"Using just CAR and CDR, is it possible to write a function that
returns the last element of a list, no matter how long the list is?
Explain."

* Do we assume the Lisp supports tail-call optimization?
* Are we assuming the list has an end? After all, they say, "no matter
how long the list is."
* Why not just be a little precise in a footnote for the hapless
newbie, and say what operators you're allowed to use?

Tayssir

--

"It is understood that the methods of propaganda can be effective only
with the voter who makes up his own mind on the basis of his group
prejudices and desires. Where specific allegiances and loyalties exist,
as in the case of boss leadership, these loyalties will operate to
nullify the free will of the voter. [...] In actual fact, it can be
done only by meeting the conditions of the public mind, by creating
circumstances which set up trains of thought, by dramatizing
personalities, by establishing contact with the group leaders who
control the opinions of their publics."

-- Edward Bernays, _Propaganda_ 1928
http://www.pentaside.org/article/propaganda-bernays-1928.html
From: Dave Seaman
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <e3nrhh$acl$1@mailhub227.itcs.purdue.edu>
On 8 May 2006 09:14:05 -0700, Tayssir John Gabbour wrote:
> Dave Seaman wrote:
>> That's not the conclusion I drew from the answer.  I thought the question
>> was asking for a function definition whose BODY uses only CAR and CDR,
>> and no other functions.  Therefore, DEFUN is allowed (because it doesn't
>> appear in the function body), but not conditionals such as IF.

> I remember disliking these questions. It was:

> "Using just CAR and CDR, is it possible to write a function that
> returns the last element of a list, no matter how long the list is?
> Explain."

> * Do we assume the Lisp supports tail-call optimization?

I don't see how that applies to any function that is defined using only
CAR and CDR.

> * Are we assuming the list has an end? After all, they say, "no matter
> how long the list is."

This is not relevant, since the answer to the question is "no" even when
restricted to lists that have an end.

> * Why not just be a little precise in a footnote for the hapless
> newbie, and say what operators you're allowed to use?

Precision is never a bad idea, but sometimes details can be filled in by
a bit of intelligent analysis.



-- 
Dave Seaman
U.S. Court of Appeals to review three issues 
concerning case of Mumia Abu-Jamal.
<http://www.mumia2000.org/>
From: Tayssir John Gabbour
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <1147116317.858356.184570@j33g2000cwa.googlegroups.com>
Dave Seaman wrote:
> On 8 May 2006 09:14:05 -0700, Tayssir John Gabbour wrote:
> > * Are we assuming the list has an end? After all, they say, "no matter
> > how long the list is."
>
> This is not relevant, since the answer to the question is "no" even when
> restricted to lists that have an end.
> [...]
> Precision is never a bad idea, but sometimes details can be filled in by
> a bit of intelligent analysis.

In the same way you're supposed to guess what the question means, I
think you're supposed to guess than an answer is supposed to contain
more than "no." You may have a range of specific answers, all more
interesting than what the guy has in the back of the book, and you're
not sure which one is appropriate, or when you should stop your
personal search for an answer.

You might put yourself in the shoes of someone learning Lisp, reading
one of those typical books with the boring CAR/CDR stuff in the
beginning. What are the annoyances which lead to dissatisfaction with
such questions?

* You hear about recursion blowing the stack, yet the book blithely
teaches you recursion to deal with arbitrarily long lists. When you
encounter poorly-specified questions, it appears this is because the
author is a poor thinker and poor coder. (The vaunted "Lisp geniuses"
we hear about seem to be coasting on reputation here.) When the book
starts being a bit intelligent, we can then reciprocate with a bit of
intelligent analysis.

* "If Lisp's main strength is macros, and Scheme is the best Lisp to
learn, and SICP is the best book on Scheme... I'll bet SICP'll teach me
all about macros!"

* Common Lisp programmers apparently are blinded by CAR/CDR/recursion.
Which is kinda embarrassing, since there's that recursion problem...

* "Mutable programming language that can assimilate other languages?
When will they start showing code without all those parentheses...?"


These are often just bad books. The real answer to the question is to
close the book and read _Practical Common Lisp_, which knows better
than to quiz people with artificial homework which has little to do
with serious programming and does little to motivate people.


Tayssir
From: Bill Atkins
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <871wv41fe2.fsf@rpi.edu>
"Tayssir John Gabbour" <···········@yahoo.com> writes:

> Dave Seaman wrote:
>> On 8 May 2006 09:14:05 -0700, Tayssir John Gabbour wrote:
>> > * Are we assuming the list has an end? After all, they say, "no matter
>> > how long the list is."
>>
>> This is not relevant, since the answer to the question is "no" even when
>> restricted to lists that have an end.
>> [...]
>> Precision is never a bad idea, but sometimes details can be filled in by
>> a bit of intelligent analysis.
>
> In the same way you're supposed to guess what the question means, I
> think you're supposed to guess than an answer is supposed to contain
> more than "no." You may have a range of specific answers, all more
> interesting than what the guy has in the back of the book, and you're
> not sure which one is appropriate, or when you should stop your
> personal search for an answer.
>
> You might put yourself in the shoes of someone learning Lisp, reading
> one of those typical books with the boring CAR/CDR stuff in the
> beginning. What are the annoyances which lead to dissatisfaction with
> such questions?
>
> * You hear about recursion blowing the stack, yet the book blithely
> teaches you recursion to deal with arbitrarily long lists. When you
> encounter poorly-specified questions, it appears this is because the
> author is a poor thinker and poor coder. (The vaunted "Lisp geniuses"
> we hear about seem to be coasting on reputation here.) When the book
> starts being a bit intelligent, we can then reciprocate with a bit of
> intelligent analysis.
>
> * "If Lisp's main strength is macros, and Scheme is the best Lisp to
> learn, and SICP is the best book on Scheme... I'll bet SICP'll teach me
> all about macros!"
>
> * Common Lisp programmers apparently are blinded by CAR/CDR/recursion.
> Which is kinda embarrassing, since there's that recursion problem...
>
> * "Mutable programming language that can assimilate other languages?
> When will they start showing code without all those parentheses...?"
>
>
> These are often just bad books. The real answer to the question is to
> close the book and read _Practical Common Lisp_, which knows better
> than to quiz people with artificial homework which has little to do
> with serious programming and does little to motivate people.

Hear, hear!

-- 
This is a song that took me ten years to live and two years to write.
 - Bob Dylan
From: Dave Seaman
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <e3oa3c$k0r$1@mailhub227.itcs.purdue.edu>
On 8 May 2006 12:25:17 -0700, Tayssir John Gabbour wrote:
> Dave Seaman wrote:
>> On 8 May 2006 09:14:05 -0700, Tayssir John Gabbour wrote:
>> > * Are we assuming the list has an end? After all, they say, "no matter
>> > how long the list is."

>> This is not relevant, since the answer to the question is "no" even when
>> restricted to lists that have an end.
>> [...]
>> Precision is never a bad idea, but sometimes details can be filled in by
>> a bit of intelligent analysis.

> In the same way you're supposed to guess what the question means, I
> think you're supposed to guess than an answer is supposed to contain
> more than "no." 

I left out the "because" part of the answer only in order to avoid
redundancy.  It has already been explained.

>You may have a range of specific answers, all more
> interesting than what the guy has in the back of the book, and you're
> not sure which one is appropriate, or when you should stop your
> personal search for an answer.

> You might put yourself in the shoes of someone learning Lisp, reading
> one of those typical books with the boring CAR/CDR stuff in the
> beginning. What are the annoyances which lead to dissatisfaction with
> such questions?

I didn't say the book was blameless.  I merely pointed out that two
particular objections were groundless.

> * You hear about recursion blowing the stack, yet the book blithely
> teaches you recursion to deal with arbitrarily long lists. When you
> encounter poorly-specified questions, it appears this is because the
> author is a poor thinker and poor coder. (The vaunted "Lisp geniuses"
> we hear about seem to be coasting on reputation here.) When the book
> starts being a bit intelligent, we can then reciprocate with a bit of
> intelligent analysis.

Recursion was specifically excluded from this question.  There are indeed
objections that can be raised here, but people keep raising the wrong
objections.

> * "If Lisp's main strength is macros, and Scheme is the best Lisp to
> learn, and SICP is the best book on Scheme... I'll bet SICP'll teach me
> all about macros!"

> * Common Lisp programmers apparently are blinded by CAR/CDR/recursion.
> Which is kinda embarrassing, since there's that recursion problem...

> * "Mutable programming language that can assimilate other languages?
> When will they start showing code without all those parentheses...?"


> These are often just bad books. The real answer to the question is to
> close the book and read _Practical Common Lisp_, which knows better
> than to quiz people with artificial homework which has little to do
> with serious programming and does little to motivate people.

That's better.




-- 
Dave Seaman
U.S. Court of Appeals to review three issues 
concerning case of Mumia Abu-Jamal.
<http://www.mumia2000.org/>
From: doug
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <1147129670.841330.323420@i40g2000cwc.googlegroups.com>
Wow, I learned more about CL by reading this thread.  Thanks for
everyone that chimed in!

Now speaking of this CL book I am reading, it is:
"COMMON LISP: A Gentle Introduction to Symbolic Computation".
by David S. Touretzky

To be fair to the author, I didn't give the complete answer but I
didn't understand the 2nd half of it:

"The function needs to keep taking successive CDRs until it reaches a
cell whose CDR is NIL; then
it should return the CAR of the cell.  We'll learn how to do this in
Chapter 8".

You see, to a newbie of lisp like me, the 2nd half of answer seems to
contradict the 1st half.  Now
I see his point after reading all your inputs, i.e. he really meant to
"just using CDR and CAR" and
nothing else!

Many of you commented on language teaching.  I actually came to this
book after studying the first 3
chapters of "Pratical Common Lisp".  The PCL book seems to be very
dense in the begining because
it glossed over lots of language details in the example code in the
first 3 chapters.  With that said, I
plan to go back to the PCL book after I finish Touretzky's book.

Best regards,

Doug
From: Tayssir John Gabbour
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <1147130116.237832.213540@u72g2000cwu.googlegroups.com>
Dave Seaman wrote:
> Recursion was specifically excluded from this question.  There are indeed
> objections that can be raised here, but people keep raising the wrong
> objections.

Whatever the merits of the question were, and perhaps it makes more
sense in Touretsky's context, "specifically" excluding things wasn't
one of its strong suits. ;)

"Using just CAR and CDR, is it possible to write a function that
returns the last element of a list, no matter how long the list is?
Explain."


Tayssir
From: Dave Seaman
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <e3ov3b$vgt$2@mailhub227.itcs.purdue.edu>
On 8 May 2006 16:15:16 -0700, Tayssir John Gabbour wrote:
> Dave Seaman wrote:
>> Recursion was specifically excluded from this question.  There are indeed
>> objections that can be raised here, but people keep raising the wrong
>> objections.

> Whatever the merits of the question were, and perhaps it makes more
> sense in Touretsky's context, "specifically" excluding things wasn't
> one of its strong suits. ;)

> "Using just CAR and CDR, is it possible to write a function that
> returns the last element of a list, no matter how long the list is?
> Explain."

I'm willing to be proven wrong.  Show me how you can write a recursive
function using only CAR and CDR.



-- 
Dave Seaman
U.S. Court of Appeals to review three issues 
concerning case of Mumia Abu-Jamal.
<http://www.mumia2000.org/>
From: Tayssir John Gabbour
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <1147144272.829030.239400@j73g2000cwa.googlegroups.com>
Dave Seaman wrote:
> I'm willing to be proven wrong.

I'm sure we're all aware of the sad fact that we can be proven wrong
regardless of how willing we are. Or how much we accept the proof.


> Show me how you can write a recursive
> function using only CAR and CDR.

You mean also given the ability to use DEFUN and a few lists?

(defun |CAR and CDR| ()
  (|CAR and CDR|))

Actually, the real answer to your question (and possibly to the
question which started the thread) is "mu".
http://www.catb.org/jargon/html/M/mu.html


Tayssir
From: Dave Seaman
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <e3p2la$2ao$2@mailhub227.itcs.purdue.edu>
On 8 May 2006 20:11:12 -0700, Tayssir John Gabbour wrote:
> Dave Seaman wrote:
>> I'm willing to be proven wrong.

> I'm sure we're all aware of the sad fact that we can be proven wrong
> regardless of how willing we are. Or how much we accept the proof.


>> Show me how you can write a recursive
>> function using only CAR and CDR.

> You mean also given the ability to use DEFUN and a few lists?

> (defun |CAR and CDR| ()
>   (|CAR and CDR|))

> Actually, the real answer to your question (and possibly to the
> question which started the thread) is "mu".
> http://www.catb.org/jargon/html/M/mu.html


I see nothing wrong with the question that started the thread.  It has been
answered.



-- 
Dave Seaman
U.S. Court of Appeals to review three issues 
concerning case of Mumia Abu-Jamal.
<http://www.mumia2000.org/>
From: Tayssir John Gabbour
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <1147148958.438446.34430@i39g2000cwa.googlegroups.com>
Dave Seaman wrote:
> On 8 May 2006 20:11:12 -0700, Tayssir John Gabbour wrote:
> > Actually, the real answer to your question (and possibly to the
> > question which started the thread) is "mu".
> > http://www.catb.org/jargon/html/M/mu.html
>
> I see nothing wrong with the question that started the thread.  It has been
> answered.

Yes, mu is indeed a versatile answer. Incidentally, I'm curious
whether, in the alternate universe where the author "specifically"
banned recursion (that is, he didn't allow the function-name to appear
in the body of the defun form), we must also ban any normal symbol from
the lambda-list from appearing there too (unless it's either the symbol
car or cdr, which would be unreadable and bad for education).

Tayssir
From: Bill Atkins
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <87ac9rpjp1.fsf@rpi.edu>
Dave Seaman <·······@no.such.host> writes:

> On 8 May 2006 20:11:12 -0700, Tayssir John Gabbour wrote:
>> Dave Seaman wrote:
>>> I'm willing to be proven wrong.
>
>> I'm sure we're all aware of the sad fact that we can be proven wrong
>> regardless of how willing we are. Or how much we accept the proof.
>
>
>>> Show me how you can write a recursive
>>> function using only CAR and CDR.
>
>> You mean also given the ability to use DEFUN and a few lists?
>
>> (defun |CAR and CDR| ()
>>   (|CAR and CDR|))
>
>> Actually, the real answer to your question (and possibly to the
>> question which started the thread) is "mu".
>> http://www.catb.org/jargon/html/M/mu.html
>
>
> I see nothing wrong with the question that started the thread.  It has been
> answered.

I think there's lots of room for improvement.  The question might have
been better phrased as:

"You can obtain the last element of a two-element list with:

  (defun last-of-2 (list)
    (car (cdr list)))

the last of a 3-element list with:

  (defun last-of-3 (list)
    (car (cddr list)))

and so on.

Write a function that will return the last element of an
arbitrary-length list using the same operators used in these functions
(i.e. assuming only defun, car, and compositions of cdr's).  If you
don't think this can be done, explain why."

-- 
This is a song that took me ten years to live and two years to write.
 - Bob Dylan
From: Marcin 'Qrczak' Kowalczyk
Subject: Re: Timed Unix process for CMUCL
Date: 
Message-ID: <87zmhp60qg.fsf@qrnik.zagroda>
Fred Gilham <······@snapdragon.csl.sri.com> writes:

> (alien:def-alien-routine ("wait4" c-wait4) c-call:int
>   (wpid c-call:int)
>   (status c-call:int :out)
>   (options c-call:int)
>   (rusage c-call:int))

The first argument and the result is a pid_t, not an int.
(It might have a different size.)

The last argument is a pointer to struct rusage, not an int.

-- 
   __("<         Marcin Kowalczyk
   \__/       ······@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/
From: Pascal Bourguignon
Subject: Re: newbie question on CAR and CDR in CL
Date: 
Message-ID: <87u080shii.fsf@thalassa.informatimago.com>
Dave Seaman <·······@no.such.host> writes:
> I think the question used "list" and not "s-expression" precisely because
> they had proper lists in mind.

Yes, that's my whole point: teachers ask questions given what they
have in mind as an answer.  Often, if you don't guess the answer, it's
hard or even impossible to understand the question.


This is not necessarily a bad thing.  The world's not fair.  

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

PLEASE NOTE: Some quantum physics theories suggest that when the
consumer is not directly observing this product, it may cease to
exist or will exist only in a vague and undetermined state.