Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion cadr vs cdr in describe-location
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
richard kappler  
View profile  
 More options Jul 22 2012, 12:58 pm
From: richard kappler <richkapp...@gmail.com>
Date: Sun, 22 Jul 2012 12:58:32 -0400
Local: Sun, Jul 22 2012 12:58 pm
Subject: Re: cadr vs cdr in describe-location

Craig, Mauricio, your explanations illustrated it beautifully, Now I
understand, thank you.

regards, Richard

On Sun, Jul 22, 2012 at 12:50 PM, Mauricio <maufd...@gmail.com> wrote:
> About your question regarding using cdr instead of cadr since there are
> only two elements in the list, that is not quite the way it works, you have
> to remember that a list is just a bunch of cons cell, the first (car) has
> an element and the second (cdr) points to the car of the next cons cell,
> the last cdr in the last cons in the list point to nil, and that is what
> tells lisp it has reached the end of the list.

> When you do cadr, you get the element which is in the first element of the
> cons cell, in this case a list of symbols which forms the description,
> while if you do cdr you get the a list with another list inside it which is
> the list you were interested in.

> To illustrate better just evaluate the following two lines in your repl.

> [1]> (cdr '(1 '(1 3 4)))
> ('(1 3 4))
> [2]> (cadr '(1 '(1 3 4)))
> '(1 3 4)

> The above is directly copied from my CLISP repl. As you can see the
> results are different. I always try to remember that the list is just a
> construct made of cons cells, and that helps me get my mind around the
> car/cdr combinations.

> Also if you want shorthand for the second element in this case you can use
> second instead of cadr.

> [3]> (second '(1 '(1 3 4)))
> '(1 3 4)

> Hope I understood your question, and if so that my answer was clear.

> El sábado, 21 de julio de 2012 10:39:29 UTC-6, richard kappler escribió:

>> Some questions brought up by the text game in ch 5, particularly the
>> describe-location function:

>> (defun describe-location (location nodes)
>>     (cadr (assoc location nodes)))

>> As I understand it, what's happening is we're defining a function named
>> describe-location that takes location and nodes as arguments, so for
>> example if I call
>> (describe-location 'garden *nodes*) it goes to the *nodes* parameter,
>> pulls out the 'garden bit (garden (you are in a beautiful garden. there is
>> a well in front of you)) and lops off "(garden " because of the cadr
>> returning only (you are in a beautiful garden. there is a well in front of
>> you)

>> 1.  I think I get the read vs evaluate bit, but it's rather tenuous.  Am
>> I correct that first Lisp reads the whole thing, then evaluates from the
>> inside out [first finds, (assoc location nodes) then "cadr's" it then
>> returns the result as the value of describe location]?

>> 2.  cadr is the same as (car (cdr ()) which is the same as from the rest
>> take the first which is equivalent to get rid of the first symbol and from
>> the rest take the first?

>> 3.  Do we use cadr because its just less typing (more elegant and
>> downright cool?) than (first (rest())?

>> 4.  Why not just use cdr since, in this case anyway, we know there are
>> only two symbols, the location and the description.  Would cdr not do the
>> same as cadr in this instance?

>> regards, Richard

--
"Treat all disasters as if they were trivialities but never treat a
triviality as if it were a disaster."
       -- *Quentin Crisp<http://www.quotationspage.com/quotes/Quentin_Crisp>
*

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.