emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [External] : Add hints to documentation of car and cdr for (e)lisp n


From: Drew Adams
Subject: RE: [External] : Add hints to documentation of car and cdr for (e)lisp newcomers - take 2
Date: Wed, 14 Jul 2021 16:37:53 +0000

> How about:
> 
>   ,----
>   | (car '(a . b))
>   |
>   | Return a.  If arg is nil, return nil.
>   `----
>   ,----
>   | (cdr '(a . b))
>   |
>   | Return b.  If arg is nil, return nil.
>   `----
>
> Or what about:
> 
>   ,----
>   | (car LIST)
>   |
>   | Return the car of LIST, eg if LIST is '(a . b) return a.  If arg is nil,
> return nil.
>   `----
> 
>   ,----
>   | (cdr LIST)
>   |
>   | Return the cdr of LIST, eg if LIST is '(a . b) return b.  If arg is nil,
> return nil.
>   `----


Allow Harold Abelson to present it:

https://www.youtube.com/watch?v=DrFkf-T-6Co

See 19:12 minutes into the recording.
In particular this, at 22:44:

  For any x and y:

    (car (cons x y)) is x
    (cdr (cons x y)) is y

That's it.  Starting from knowing _nothing_
about cons, car, and cdr, that pretty much
tells you everything.  It _defines_ all 3.

(This is an algebraic specification of an
abstract data type, where "cons" is an
_unspecified_ function (function symbol),
aka a "constructor".  It defines "car" and
"cdr", and it leaves "cons" undefined.)

Except that definition leaves out the nil
case (in most Lisps, including Elisp),
which is defined by this:

    (car nil) is nil
    (cdr nil) is nil

(And except for the case where "any x or
y" is, in effect, "bottom": where trying
to evaluate their sexps produces no x or
y _value_ - raises an error or doesn't
terminate.)

You don't need to know anything else about
what cons or nil are or might be, to
understand them, car, and cdr in the
context of pairs (and lists).

Sure, to know more about using them
practically you'll want to know about `eq'
and other things.  But as for what cons,
car, and cdr _are_ (and do), that's it.

(What you said is correct, but as others
have mentioned, the notation (a . b) needs
to first be understood.)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]