[racket] first vs car ; last vs cdr ; empty? vs null?
On Mar 7, 2014, at 2:45, Daniel Carrera <dcarrera at gmail.com> wrote:
> Hello,
>
> Is there any difference between `first` and `car`, or between `last` and `cdr`, or between `empty? and null?` ?
>
> I had assumed that these were just synonyms, added by Racket because they might be more memorable to a student. But apparently Racket doesn't think they are equal:
>
> -> (equal? first car)
> #f
> -> (equal? last cdr)
> #f
> -> (equal? empty? null?)
> #f
>
>
> I suppose that they could be separate functions that happen to do the same thing, but if so, my next question would be why they aren't just aliases. As in:
>
> -> (define myfirst car)
> -> (equal? myfirst car)
For many/most things in racket, you can bring up the definition for something inside of DrRacket:
(define (first x)
(if (and (pair? x) (list? x))
(car x)
(raise-argument-error 'first "(and/c list? (not/c empty?))" x)))
I couldn't for car, so I'm assuming it is considered a primitive.
last and cdr aren't synonymous:
(define (last l)
(if (and (pair? l) (list? l))
(let loop ([l l] [x (cdr l)])
(if (pair? x)
(loop x (cdr x))
(car l)))
(raise-argument-error 'last "(and/c list? (not/c empty?))" l)))
(define (empty? l) (null? l))
null? seems to be a primitive as well. Not sure why they're not direct synonyms in this case.