<div dir="ltr">I just discovered that SRFI-1 defines first, second, third, ... eight. Except that first = car, second = cadr, and so on.<div><div><br></div><div>Cheers,</div><div>Daniel.</div></div></div><div class="gmail_extra">
<br><br><div class="gmail_quote">On 7 March 2014 19:57, Matthias Felleisen <span dir="ltr"><<a href="mailto:matthias@ccs.neu.edu" target="_blank">matthias@ccs.neu.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
I prefer first/rest for readability.<br>
<br>
When I write (first (second (first x))) though, my old Lisp heart comes back and I switch to (caadar x) or something like that.<br>
<br>
<br>
<br>
<br>
On Mar 7, 2014, at 1:40 PM, Daniel Carrera <<a href="mailto:dcarrera@gmail.com">dcarrera@gmail.com</a>> wrote:<br>
<br>
> On the contrary, car and cdr are GUARANTEED to be O(1).<br>
><br>
> car == return the first element in a pair.<br>
> cdr == return the second element in a pair.<br>
><br>
> The fact that the cdr of a list returns the rest of the list is simply an incidental side-effect of the definition of list.<br>
><br>
> Cheers,<br>
> Daniel.<br>
><br>
><br>
> On 7 March 2014 18:28, Deren Dohoda <<a href="mailto:deren.dohoda@gmail.com">deren.dohoda@gmail.com</a>> wrote:<br>
> Does this mean we shouldn't cdr functional lists but only use rest?<br>
><br>
> On Mar 7, 2014 12:02 PM, "Matthias Felleisen" <<a href="mailto:matthias@ccs.neu.edu">matthias@ccs.neu.edu</a>> wrote:<br>
><br>
> In a world of immutable cons-es you can cache the result so that testing list? becomes an O(1) operation.<br>
><br>
><br>
><br>
><br>
> On Mar 7, 2014, at 11:52 AM, Eric Dong <<a href="mailto:yd2dong@uwaterloo.ca">yd2dong@uwaterloo.ca</a>> wrote:<br>
><br>
> > Forgive me if I am super terribly wrong. Isn't it the case that an improper list is only known to be improper if we walk to the end and find something other than an empty? So wouldn't that mean "first" and "rest" take linear time since they must make sure the argument is a list? Clearly that doesn't happen. What am I missing?<br>
> ><br>
> ><br>
> > On Fri, Mar 7, 2014 at 10:16 AM, Daniel Carrera <<a href="mailto:dcarrera@gmail.com">dcarrera@gmail.com</a>> wrote:<br>
> > Ok. That makes sense. A list is either '() or something plus a list.<br>
> ><br>
> > Thanks.<br>
> ><br>
> > Cheers,<br>
> > Daniel.<br>
> ><br>
> ><br>
> > On 7 March 2014 14:46, Jon Zeppieri <<a href="mailto:zeppieri@gmail.com">zeppieri@gmail.com</a>> wrote:<br>
> > Oops, sorry about that empty message. I was going to say that your<br>
> > definition of a list is close, but it's missing something, A list is<br>
> > either:<br>
> ><br>
> > - the empty list; or<br>
> > - a pair, the second element of which is a list<br>
> ><br>
> > (cons 3 2) is a pair, and sometimes non-list pairs are called<br>
> > "improper lists," but they don't satisfy list?.<br>
> ><br>
> ><br>
> > On Fri, Mar 7, 2014 at 8:43 AM, Jon Zeppieri <<a href="mailto:zeppieri@gmail.com">zeppieri@gmail.com</a>> wrote:<br>
> > > On Fri, Mar 7, 2014 at 8:39 AM, Daniel Carrera <<a href="mailto:dcarrera@gmail.com">dcarrera@gmail.com</a>> wrote:<br>
> > >> What is (cons 3 2) ? What is the definition of a list? I thought that a list<br>
> > >> was defined as either '() or a pair.<br>
> > >><br>
> > >> Cheers,<br>
> > >> Daniel.<br>
> > >><br>
> > >><br>
> > >> On 7 March 2014 13:49, Jens Axel Søgaard <<a href="mailto:jensaxel@soegaard.net">jensaxel@soegaard.net</a>> wrote:<br>
> > >>><br>
> > >>> The value (cons 3 42) is not a list. The function car will extract 3,<br>
> > >>> but first will fail.<br>
> > >>><br>
> > >>> /Jens Axel<br>
> > >>><br>
> > >>><br>
> > >>> 2014-03-07 13:40 GMT+01:00 Daniel Carrera <<a href="mailto:dcarrera@gmail.com">dcarrera@gmail.com</a>>:<br>
> > >>> > Thanks. That's a very useful tip (being able to get at the source code).<br>
> > >>> > I<br>
> > >>> > am a bit confused by the condition "(and (pair? x) (list? x))". It seems<br>
> > >>> > to<br>
> > >>> > me that this could just be replaced with "(pair? x)". The "list?"<br>
> > >>> > doesn't<br>
> > >>> > add anything. Am I wrong?<br>
> > >>> ><br>
> > >>> > Also, I don't see exactly how "first" and "car" behave different on a<br>
> > >>> > non-list. They both raise an error. The errors are just worded<br>
> > >>> > differently.<br>
> > >>> ><br>
> > >>> > On the same file, I found the definition of empty?<br>
> > >>> ><br>
> > >>> > (define empty? (lambda (l) (null? l)))<br>
> > >>> ><br>
> > >>> > Wouldn't it be more economical to write "(define empty? null?)" and<br>
> > >>> > allow<br>
> > >>> > them to be synonyms?<br>
> > >>> ><br>
> > >>> > Cheers,<br>
> > >>> > Daniel.<br>
> > >>> ><br>
> > >>> ><br>
> > >>> > On 7 March 2014 12:16, Jens Axel Søgaard <<a href="mailto:jensaxel@soegaard.net">jensaxel@soegaard.net</a>> wrote:<br>
> > >>> >><br>
> > >>> >> For lists first/rest works the same as car/cdr.<br>
> > >>> >> For non-lists there is a difference: first and rest signals an error.<br>
> > >>> >> The names first and rest makes it easier for a human reader of<br>
> > >>> >> a piece of code to see that the program works on lists only.<br>
> > >>> >><br>
> > >>> >> For the curious, the definition of first is:<br>
> > >>> >><br>
> > >>> >> (define (first x)<br>
> > >>> >> (if (and (pair? x) (list? x))<br>
> > >>> >> (car x)<br>
> > >>> >> (raise-argument-error 'first "(and/c list? (not/c empty?))" x)))<br>
> > >>> >><br>
> > >>> >> I found this definition like this:<br>
> > >>> >> 1. Entered this program in DrRacket:<br>
> > >>> >> #lang racket<br>
> > >>> >> first<br>
> > >>> >> 2. Clicked the "Check Syntax" button<br>
> > >>> >> 3. Right clicked the identifier first and chose "Open defining file"<br>
> > >>> >> 4. Chose "first" in the definition-drop-down in the upper left corner.<br>
> > >>> >><br>
> > >>> >> /Jens Axel<br>
> > >>> >><br>
> > >>> >><br>
> > >>> >><br>
> > >>> >><br>
> > >>> >><br>
> > >>> >><br>
> > >>> >><br>
> > >>> >> 2014-03-07 11:45 GMT+01:00 Daniel Carrera <<a href="mailto:dcarrera@gmail.com">dcarrera@gmail.com</a>>:<br>
> > >>> >> > Hello,<br>
> > >>> >> ><br>
> > >>> >> > Is there any difference between `first` and `car`, or between `last`<br>
> > >>> >> > and<br>
> > >>> >> > `cdr`, or between `empty? and null?` ?<br>
> > >>> >> ><br>
> > >>> >> > I had assumed that these were just synonyms, added by Racket because<br>
> > >>> >> > they<br>
> > >>> >> > might be more memorable to a student. But apparently Racket doesn't<br>
> > >>> >> > think<br>
> > >>> >> > they are equal:<br>
> > >>> >> ><br>
> > >>> >> > -> (equal? first car)<br>
> > >>> >> > #f<br>
> > >>> >> > -> (equal? last cdr)<br>
> > >>> >> > #f<br>
> > >>> >> > -> (equal? empty? null?)<br>
> > >>> >> > #f<br>
> > >>> >> ><br>
> > >>> >> ><br>
> > >>> >> > I suppose that they could be separate functions that happen to do the<br>
> > >>> >> > same<br>
> > >>> >> > thing, but if so, my next question would be why they aren't just<br>
> > >>> >> > aliases. As<br>
> > >>> >> > in:<br>
> > >>> >> ><br>
> > >>> >> > -> (define myfirst car)<br>
> > >>> >> > -> (equal? myfirst car)<br>
> > >>> >> > #t<br>
> > >>> >> ><br>
> > >>> >> > Cheers,<br>
> > >>> >> > Daniel.<br>
> > >>> >> > --<br>
> > >>> >> > When an engineer says that something can't be done, it's a code<br>
> > >>> >> > phrase<br>
> > >>> >> > that<br>
> > >>> >> > means it's not fun to do.<br>
> > >>> >> ><br>
> > >>> >> > ____________________<br>
> > >>> >> > Racket Users list:<br>
> > >>> >> > <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
> > >>> >> ><br>
> > >>> >><br>
> > >>> >><br>
> > >>> >><br>
> > >>> >> --<br>
> > >>> >> --<br>
> > >>> >> Jens Axel Søgaard<br>
> > >>> ><br>
> > >>> ><br>
> > >>> ><br>
> > >>> ><br>
> > >>> > --<br>
> > >>> > When an engineer says that something can't be done, it's a code phrase<br>
> > >>> > that<br>
> > >>> > means it's not fun to do.<br>
> > >>><br>
> > >>><br>
> > >>><br>
> > >>> --<br>
> > >>> --<br>
> > >>> Jens Axel Søgaard<br>
> > >><br>
> > >><br>
> > >><br>
> > >><br>
> > >> --<br>
> > >> When an engineer says that something can't be done, it's a code phrase that<br>
> > >> means it's not fun to do.<br>
> > >><br>
> > >> ____________________<br>
> > >> Racket Users list:<br>
> > >> <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
> > >><br>
> ><br>
> ><br>
> ><br>
> > --<br>
> > When an engineer says that something can't be done, it's a code phrase that means it's not fun to do.<br>
> ><br>
> > ____________________<br>
> > Racket Users list:<br>
> > <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
> ><br>
> ><br>
> > ____________________<br>
> > Racket Users list:<br>
> > <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
><br>
><br>
> ____________________<br>
> Racket Users list:<br>
> <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
><br>
> ____________________<br>
> Racket Users list:<br>
> <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
><br>
><br>
><br>
<span class="HOEnZb"><font color="#888888">><br>
> --<br>
> When an engineer says that something can't be done, it's a code phrase that means it's not fun to do.<br>
<br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br>When an engineer says that something can't be done, it's a code phrase that means it's not fun to do.<br>
</div>