[racket] first vs car ; last vs cdr ; empty? vs null?

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Fri Mar 7 06:16:21 EST 2014

For lists first/rest works the same as car/cdr.
For non-lists there is a difference: first and rest signals an error.
The names first and rest makes it easier for a human reader of
a piece of code to see that the program works on lists only.

For the curious, the definition of first is:

(define (first x)
  (if (and (pair? x) (list? x))
    (car x)
    (raise-argument-error 'first "(and/c list? (not/c empty?))" x)))

I found this definition like this:
1. Entered this program in DrRacket:
      #lang racket
      first
2. Clicked the "Check Syntax" button
3. Right clicked the identifier first and chose "Open defining file"
4. Chose "first" in the definition-drop-down in the upper left corner.

/Jens Axel







2014-03-07 11:45 GMT+01:00 Daniel Carrera <dcarrera at gmail.com>:
> 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)
> #t
>
> Cheers,
> Daniel.
> --
> When an engineer says that something can't be done, it's a code phrase that
> means it's not fun to do.
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>



-- 
--
Jens Axel Søgaard


Posted on the users mailing list.