cadr vs cdr in describe-location

70 views
Skip to first unread message

richard kappler

unread,
Jul 21, 2012, 12:39:29 PM7/21/12
to land-o...@googlegroups.com
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

Purity Control

unread,
Jul 21, 2012, 2:56:27 PM7/21/12
to land-o...@googlegroups.com
Hi Richard

In response to your points

1. Lisp acts like maths so inner bracketed stuff is evaluated first. However there are exceptions to what gets evaluated for special forms. So in an if the second statement doesn't get evaluated if its true and vice versa.

2.yes

3. car and cdr are used as a historic artefact. They refer to the registers used on the machine to take the head of the list and the tail on the first lisp implementation and its stuck. Some people prefer first and rest because they are more explicit. Others prefer car and cdr (I believe I have read in the dim and distant past that Paul Graham prefers car and cdr because they are the same length and line up better in code making his code more readable).

4. because they are different. The cdr returns a list where taking the car of the cdr returns an atom.

Hope this helps.

Mauricio

unread,
Jul 22, 2012, 12:50:27 PM7/22/12
to land-o...@googlegroups.com
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. 

richard kappler

unread,
Jul 22, 2012, 12:58:32 PM7/22/12
to land-o...@googlegroups.com
Craig, Mauricio, your explanations illustrated it beautifully, Now I understand, thank you.

regards, Richard
--
"Treat all disasters as if they were trivialities but never treat a triviality as if it were a disaster." 
       -- Quentin Crisp

Reply all
Reply to author
Forward
0 new messages