help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: How to remove verbosity from the data passing mechanism using alist


From: PJ Weisberg
Subject: Re: How to remove verbosity from the data passing mechanism using alist or plist ?
Date: Tue, 7 Dec 2010 08:55:41 -0800

On Tue, Dec 7, 2010 at 7:14 AM, Drew Adams <drew.adams@oracle.com> wrote:
>> those things that look like lists but have dots in them are
>> a result of someone abusing "cons" to make a cons cell whose
>> 'next' pointer points to something other than the next cons
>> cell in a list.
>
> Actually this is not an abuse of poor little `cons'.  ;-)
>
> The list `(x)' is in fact an example of such "abusive" behavior: the cdr
> ("next") is the symbol `nil', which is an atom, not a cons cell: `(x . nil)'.
>
> It is correct to say that cons cells are used to build lists, and that that is
> their most common use.  And that a list is either `nil' or a cons cell whose 
> cdr
> is a list.  But it is also correct that some cons cells are not lists, i.e., 
> do
> not have a list as their cdr.
>
> `cons' is untyped wrt its parameters.  It is not only the first parameter (the
> car) that need not be a list, but also the second (the cdr).

Yeah, I hesitated to use 'abuse' there, but I was tired and decided to
opt for the more colorful language.  :-)

The first thing I think of when I think of cons is a trivial example
from a college class, like "(cons 1 (cons 2 (cons 3 nil)))" to make a
3-item list.  So I usually think of cons as a way to add something to
the front of a list  (nil being the same thing as an empty list).

Basically, 'list' and 'cons' both return a cons cell.  With 'cons',
the car of the cons cell is the first argument and the cdr is the
second argument.  With 'list', the car is the first argument and the
cdr is another cons cell, whose car is the second argument and whose
cdr is another cons cell, and so on until you get to a cons cell whose
car is the last argument and whose cdr is nil.

I was trying to come up with an example to show how 'list' was like a
bunch of 'cons's, but the part of my brain that can do recursion was
in sleep mode, and then the best I came up with was

(defun my-list( &rest args )
  (if args
      (cons (car args) (eval (cons 'my-list (cdr args))))
    nil))

and I didn't think was was clarifying *anything* if I had to use eval
to while I was explaining cons.  ;-)

P.S.:  For the benefit of lisp beginners who don't know, if args is
'(1 2 3 4), then (cdr args) is '(2 3 4), and (cons 'my-list '(2 3 4))
is '(my-list 2 3 4).  Doesn't that look like a function call?  eval
treats it as such.



reply via email to

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