Received: by 10.52.88.174 with SMTP id bh14mr14920957vdb.6.1342896989135; Sat, 21 Jul 2012 11:56:29 -0700 (PDT) X-BeenThere: land-of-lisp@googlegroups.com Received: by 10.220.115.146 with SMTP id i18ls3500241vcq.0.gmail; Sat, 21 Jul 2012 11:56:28 -0700 (PDT) Received: by 10.52.94.76 with SMTP id da12mr167235vdb.4.1342896988452; Sat, 21 Jul 2012 11:56:28 -0700 (PDT) Date: Sat, 21 Jul 2012 11:56:27 -0700 (PDT) From: Purity Control To: land-of-lisp@googlegroups.com Message-Id: <8df73f21-ddc9-4973-be9d-b3f80788dc56@googlegroups.com> In-Reply-To: References: Subject: Re: cadr vs cdr in describe-location MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_301_10703823.1342896987578" ------=_Part_301_10703823.1342896987578 Content-Type: multipart/alternative; boundary="----=_Part_302_13845656.1342896987578" ------=_Part_302_13845656.1342896987578 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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. On Saturday, July 21, 2012 5:39:29 PM UTC+1, richard kappler wrote: > > 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 > ------=_Part_302_13845656.1342896987578 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
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 form= s. So in an if the second statement doesn't get evaluated if its true and v= ice 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 imp= lementation and its stuck. Some people prefer first and rest because they a= re more explicit. Others prefer car and cdr (I believe I have read in the d= im and distant past that Paul Graham prefers car and cdr because they are t= he 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.



On Saturday, July 21, 2012 5:39:29 PM UTC+1, richard kappler wrote:Some questions brought up by the text= game in ch 5, particularly the describe-location function:

<= div>(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 take= s location and nodes as arguments, so for example if I call 
(describe-location 'garden *nodes*) it goes to the *nodes* parameter, pull= s out the 'garden bit (garden (you are in a beautiful garden. there is a we= ll 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 no= des) then "cadr's" it then returns the result as the value of describe loca= tion]?

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

<= /div>
3.  Do we use cadr because its just less typing (more elegan= t and downright cool?) than (first (rest())?

4. &n= bsp;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
------=_Part_302_13845656.1342896987578-- ------=_Part_301_10703823.1342896987578--