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

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

The (define (empty? l) (null? l)) is needed due to object-name.

> (object-name null?)
'null?
> (object-name empty?)
'empty?
> (define my-empty? empty?)
> (object-name my-empty?)
'empty?

/Jens Axel


2014-03-07 12:19 GMT+01:00 Ryan Davis <zenspider at gmail.com>:
>
> 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.
>
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users



-- 
--
Jens Axel Søgaard


Posted on the users mailing list.