unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: pjb@informatimago.com (Pascal J. Bourguignon)
To: help-gnu-emacs@gnu.org
Subject: Re: Creating a list
Date: Thu, 19 Nov 2009 20:26:40 +0100	[thread overview]
Message-ID: <87y6m2gvtb.fsf@galatea.local> (raw)
In-Reply-To: 87fx8aeeh1.fsf@Traian.DecebalComp

Cecil Westerhof <Cecil@decebal.nl> writes:

> Barry Margolin <barmar@alum.mit.edu> 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	other threads:[~2009-11-19 19:26 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-19  7:38 Creating a list Cecil Westerhof
2009-11-19  8:35 ` David Kastrup
2009-11-19 10:59   ` Cecil Westerhof
2009-11-19 11:41     ` David Kastrup
2009-11-19 13:13       ` Cecil Westerhof
2009-11-19 13:47         ` Barry Margolin
2009-11-19 15:11           ` Cecil Westerhof
2009-11-19 19:26             ` Pascal J. Bourguignon [this message]
2009-11-19 13:57         ` David Kastrup
2009-11-19 15:14           ` Cecil Westerhof
2009-11-19 13:59         ` Pascal J. Bourguignon
2009-11-19 11:59   ` Gnus: Some new mails are automatically marked as 'O' Wang Lei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87y6m2gvtb.fsf@galatea.local \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).