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

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

Re: Creating a list


From: Pascal J. Bourguignon
Subject: Re: Creating a list
Date: Thu, 19 Nov 2009 20:26:40 +0100
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin)

Cecil Westerhof <address@hidden> writes:

> Barry Margolin <address@hidden> writes:
>
>>> Off course.
>>
>> I think you mean "of course".
>
> English is not my first language. I'll try to be more correct.
>
>
>>> But I have a few questions on my mind.
>>> - Why is the first shown as value and the second as _symbol_?
>>
>> Because you quoted the list containing the symbol, so it wasn't 
>> evaluated.
>
> Okay, thanks. At the moment it is not important, because I now work with
> list instead off with cons,
               of


Lisp lists are implemented with cons cells.  So when you work with
lists, in lisp, you do work with conses at the same time.  This is an
important thing to understand about lisp.

While you can develop in lisp abstract data type, where the
implementation details are hidden, and where you provide all the
functional abstraction needed, in the case of conses, and lists and
trees and any other data structure built upon them, the implementation
is left leaking, so you can pun, you can play word games with these data.


Cons level functions are: cons car cdr consp (and atom).

Symbol level functions are: null

List level functions are: list list* first second ... tenth rest endp listp.

When you write:

 (let ((la (list 1 2 3)))
    (let ((lb (cons 0 la)))
       lb))

you are punning on the fact that a lisp list is a chain of conses or
the symbol nil, and that a reference to a list is also a reference to
the first cons of the list (or nil when it's empty).   

To be consistent, you should actually write:

(require 'cl)
(let ((la (list 1 2 3)))
    (let ((lb (list* 0 la)))
       lb))


Similarly, you shouldn't write (null list), but (endp list).  Notice
that endp doesn't behave exactly like null, and that in general, when
you are processing lists, you want the meaning of endp, not that of
null.


So while it may be important for the human reader to use the functions
corresponding to the right level of the data type considered, it is
also nice to be able to change of level from time to time, and be able
to write (cddr tree) to get the children of the node tree, if we
implement these trees as lists containing the label, the color, and
the children.  Only you don't use cddr all over the program, you wrap
it in a tree-children function:

(defun make-tree (label color children)
  (list* label color children)) ; list function
(defun tree-children (tree)
  (cddr tree)) ; pun: cons function, we're considering the 
               ;      list as the sequence of conses it is.



> but how would I get it evaluated?

You can get a symbol, or any expression, evaluated by not doing what
you do to prevent it being evaluated.

Let's read again the question and the answer:

>>> - Why is the first shown as value and the second as _symbol_?
>>
>> Because you quoted the list containing the symbol, so it wasn't 
>> evaluated.


So when you quote an object it doesn't get evaluated.  How can you get
it evaluated?  By NOT quoting it.

Just try it.  Type M-x ielm RET

Then type each expression followed by a RET in the *ielm* buffer: 

(defvar current 42)
'current
current
(quote current)
current
(defun furrent () (* 21 2))
'(furrent)
(furrent)
(quote (furrent))
(furrent)


-- 
__Pascal Bourguignon__


reply via email to

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