From: rihad
Subject: caadr q'n
Date: 
Message-ID: <89809f95-54dc-4283-9a68-feadc704ac14@c19g2000prf.googlegroups.com>
Hi, I'm a CL newbie learning by Peter Seibel's free online book
"Practical Common Lisp" (http://www.gigamonkeys.com/book/). There's
one point in Chapter 12 on List Processing that really got me banging
my head against the wall, even after re-reading the chapter.

These three examples I understand perfectly:
(caar (list 1 2 3))                  ==> error
(caar (list (list 1 2) 3))           ==> 1
(cadr (list (list 1 2) (list 3 4)))  ==> (3 4)

But then comes this one:
(caadr (list (list 1 2) (list 3 4))) ==> 3
What the heck? Why not simply car -> (list 1 2), caar -> 1, and thus
caadr (list 2) ?

From: ······@gmail.com
Subject: Re: caadr q'n
Date: 
Message-ID: <c8c3a832-4a22-4bef-9874-0fe6bc99a87d@s13g2000prd.googlegroups.com>
On Mar 20, 3:23 pm, rihad <·····@mail.ru> wrote:
> Hi, I'm a CL newbie learning by Peter Seibel's free online book
> "Practical Common Lisp" (http://www.gigamonkeys.com/book/). There's
> one point in Chapter 12 on List Processing that really got me banging
> my head against the wall, even after re-reading the chapter.
>
> These three examples I understand perfectly:
> (caar (list 1 2 3))                  ==> error
> (caar (list (list 1 2) 3))           ==> 1
> (cadr (list (list 1 2) (list 3 4)))  ==> (3 4)
>
> But then comes this one:
> (caadr (list (list 1 2) (list 3 4))) ==> 3
> What the heck? Why not simply car -> (list 1 2), caar -> 1, and thus
> caadr (list 2) ?

CL-USER> (car (list 1 2))
1
CL-USER> (cdr (list 1 2))
(2)
CL-USER> (cadr (list 1 2))
2

you should first learn what car/cdr means.
Best,Milan
From: rihad
Subject: Re: caadr q'n
Date: 
Message-ID: <c8dc4be9-5a55-4580-b554-e6321e405450@z38g2000hsc.googlegroups.com>
Oh, indeed, I missed the part in the text saying that caadr should
start its lisp processing _backwards_:

(cadadr list) === (car (cdr (car (cdr list))))

Thanks.
From: Kent M Pitman
Subject: Re: caadr q'n
Date: 
Message-ID: <uk5jxv1gj.fsf@nhplace.com>
rihad <·····@mail.ru> writes:

> Oh, indeed, I missed the part in the text saying that caadr should
> start its lisp processing

"list processing".  Lisp is the whole language. :)

> _backwards_:

Well, it's not really "backwards", though it may feel that way.

> (cadadr list) === (car (cdr (car (cdr list))))
                      ^    ^    ^    ^
Right...              |    |    |    |
                 c    a    d    a    d    r

... or what you would write in some other language as car(cdr(car(cdr(x)))).

In a conventional language, do you think of f(g(h(x))) as processing 
backwards?  Most people don't refer to that as backwards, even though
textually it is h before g before f.  They usually say "from the inside
out", since in fact at any given level, it goes forward (left to right),
as in:  f(g(x),h(x)) which calls g first, h next, and f last.
From: Barry Margolin
Subject: Re: caadr q'n
Date: 
Message-ID: <barmar-E9D544.16331620032008@newsgroups.comcast.net>
In article <·············@nhplace.com>,
 Kent M Pitman <······@nhplace.com> wrote:

> rihad <·····@mail.ru> writes:
> 
> > Oh, indeed, I missed the part in the text saying that caadr should
> > start its lisp processing
> 
> "list processing".  Lisp is the whole language. :)
> 
> > _backwards_:
> 
> Well, it's not really "backwards", though it may feel that way.
> 
> > (cadadr list) === (car (cdr (car (cdr list))))
>                       ^    ^    ^    ^
> Right...              |    |    |    |
>                  c    a    d    a    d    r
> 
> ... or what you would write in some other language as car(cdr(car(cdr(x)))).
> 
> In a conventional language, do you think of f(g(h(x))) as processing 
> backwards?  Most people don't refer to that as backwards, even though
> textually it is h before g before f.  They usually say "from the inside
> out", since in fact at any given level, it goes forward (left to right),
> as in:  f(g(x),h(x)) which calls g first, h next, and f last.

But consider that the component of a Lisp implementation that does

(print (eval (read)))

is called the read-eval-print loop, or REPL, not the PERL.  So sometimes 
we refer to things by the temporal order, not the way they're written in 
functional notation.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Don Geddis
Subject: Re: caadr q'n
Date: 
Message-ID: <87od99htkw.fsf@geddis.org>
Barry Margolin <······@alum.mit.edu> wrote on Thu, 20 Mar 2008:
> But consider that the component of a Lisp implementation that does
> (print (eval (read)))
> is called the read-eval-print loop, or REPL, not the PERL.

Are you sure?  Because I've heard of lots of people programming in Perl,
but I've never heard of anyone programming in "Repl".

        -- Don
_______________________________________________________________________________
Don Geddis                  http://don.geddis.org/               ···@geddis.org
Children are natural mimics who act like their parents despite every effort to
teach them good manners.
From: Harald Hanche-Olsen
Subject: Re: caadr q'n
Date: 
Message-ID: <pcobq59nq28.fsf@shuttle.math.ntnu.no>
+ Kent M Pitman <······@nhplace.com>:

> In a conventional language, do you think of f(g(h(x))) as processing
> backwards?

I seem to remember an algebra textbook that put function applications on
the right, writing the above as (((x)h)g)f, so the composition of the
three functions could be written as h�g�f - where the dots should really
be small circles - I am trying to stay out of unicode, for fear that
you'll be spouting Chinese at me again. Or whatever it was. Anyway, it
confused the hell out of the students.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- It is undesirable to believe a proposition
  when there is no ground whatsoever for supposing it is true.
  -- Bertrand Russell
From: r_stiltskin
Subject: Re: caadr q'n
Date: 
Message-ID: <27efa034a03b8733f9f1ce96064b56ae@localhost.talkaboutprogramming.com>
Because the cadr is the car of the cdr, not the cdr of the car.  And
similarly the caadr is the car of (the car of the cdr).

--
Message posted using http://www.talkaboutprogramming.com/group/comp.lang.lisp/
More information at http://www.talkaboutprogramming.com/faq.html
From: Steven M. Haflich
Subject: Re: caadr q'n
Date: 
Message-ID: <3bHEj.14741$5K1.924@newssvr12.news.prodigy.net>
r_stiltskin wrote:
> Because the cadr is the car of the cdr, not the cdr of the car.  And
> similarly the caadr is the car of (the car of the cdr).

An easy mnemonic way to keep this straight, and to think about what code 
does, is to splice the word "of" between the levels of nested functions 
(including the car/cdr compounds).  So think of cadr as car of cdr, 
etc., and (print (eval (read)) as print of eval of read.