all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Giorgos Keramidas <keramida@ceid.upatras.gr>
To: help-gnu-emacs@gnu.org
Subject: Re: Basic Emacs Lisp question
Date: Wed, 30 Apr 2008 04:21:39 +0300	[thread overview]
Message-ID: <87tzhkkuho.fsf@kobe.laptop> (raw)
In-Reply-To: 87od7sa6ke.fsf@kobe.laptop

On Tue, 29 Apr 2008 20:56:17 +0300, Giorgos Keramidas <keramida@ceid.upatras.gr> wrote:
> On Tue, 29 Apr 2008 17:49:19 +0200, Matthias Pfeifer <pfemat@web.de> wrote:
>> Hello,
>>
>> What is the difference between
>>
>> (list 0 nil -1)
>>
>> and
>>
>> '(0 nil -1)
>
> In Common Lisp (list 0 nil -1) is required to 'cons' a new list every
> time it is called.  Quoting the list as in '(0 nil -1) is not required
> to build a new list.  In fact, in compiled code it may reuse the same
> static object over and over again.

Reading my own post reveals that I may have been too terse.  To clarify
the point I was trying to make, here's a small test in Common Lisp, and
the equivalent test in Emacs Lisp.

1. Common Lisp test
-------------------

* Save the following Lisp code to a file called "foo.lisp":

  (defun foo-quoted ()
    '(0 nil -1))

  (defun foo-list ()
    (list 0 nil -1))

* Then compile the file, and load it.  Here's the output from loading
  the compiled file in SBCL:

  CL-USER> (compile-file "foo")

  ; compiling file "/home/keramida/foo.lisp" (written 30 APR 2008 01:48:02 AM):
  ; compiling (DEFUN FOO-QUOTED ...)
  ; compiling (DEFUN FOO-LIST ...)

  ; /home/keramida/foo.fasl written
  ; compilation finished in 0:00:00
  #P"/home/keramida/foo.fasl"
  NIL
  NIL
  CL-USER> (load "foo")          ;; This actually loads "foo.fasl" in SBCL.
  T
  CL-USER>

* Every time the `foo-quoted' function runs it returns exactly the same
  compiled object.  The object returned by separate calls to
  `foo-quoted' is all of EQ, EQL and EQUAL to any previous call, as you
  can see in:

  CL-USER> (let ((one-list (foo-quoted))
                 (another-list (foo-quoted)))
             (mapcar (lambda (test)
                       (funcall test one-list another-list))
                     (list #'eq #'eql #'equal)))
  (T T T)
  CL-USER>

* In contrast, the object returned by the `foo-list' function is a newly
  CONS-ed list every time the function runs:

  CL-USER> (let ((one-list (foo-list))
                 (another-list (foo-list)))
             (mapcar (lambda (test)
                       (funcall test one-list another-list))
                     (list #'eq #'eql #'equal)))
  (NIL NIL T)
  CL-USER>

The lists returned by `foo-list' are EQUAL, but they are neither EQ nor
EQL to each other.  They are created from scratch by allocating new
storage for the value of the expression every time the `foo-list'
function is called.

2. Emacs Lisp test
------------------

* Save the same two functions in a file called "foo.el".

* Fire up Emacs, and byte-compile the file by typing

  M-x byte-compile-file RET foo.el RET

* Load the byte-compiled file by typing

  M-x load-file RET foo.elc RET

* Now evaluate the same two LET forms in your scratch buffer, by pasting
  them in the buffer and typing `C-x C-e' after each expression.

  Emacs Lisp should also evaluate them as:

  (let ((one-list (foo-quoted))
        (another-list (foo-quoted)))
    (mapcar (lambda (test)
              (funcall test one-list another-list))
            (list #'eq #'eql #'equal)))
  => (t t t)

  (let ((one-list (foo-list))
        (another-list (foo-list)))
    (mapcar (lambda (test)
              (funcall test one-list another-list))
            (list #'eq #'eql #'equal)))
  => (nil nil t)

I hope this makes what I initially wrote a bit easier to grasp :-)

Giorgos



  reply	other threads:[~2008-04-30  1:21 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-04-29 15:49 Basic Emacs Lisp question Matthias Pfeifer
2008-04-29 17:56 ` Giorgos Keramidas
2008-04-30  1:21   ` Giorgos Keramidas [this message]
2008-04-30  8:37     ` Tim X
2008-04-30 15:26   ` David Kastrup
2008-04-30 18:36     ` Giorgos Keramidas
2008-05-01  9:25       ` Johan Bockgård
2008-04-29 21:14 ` tyler
2008-04-30  8:31   ` Tim X
2008-05-05 13:38     ` tyler
  -- strict thread matches above, loose matches on Subject: below --
2014-09-09 19:35 Basic emacs lisp question Ken
2014-09-09 19:52 ` Thorsten Jolitz
2014-09-09 21:05   ` Thorsten Jolitz
     [not found]   ` <mailman.8562.1410296811.1147.help-gnu-emacs@gnu.org>
2014-09-09 23:02     ` Emanuel Berg
     [not found] ` <mailman.8561.1410292382.1147.help-gnu-emacs@gnu.org>
2014-09-09 20:07   ` Emanuel Berg
     [not found]     ` <874mwgv93m.fsf@gmail.com>
2014-09-09 20:44       ` Emanuel Berg
     [not found]         ` <87y4tsts4t.fsf@gmail.com>
2014-09-09 22:31           ` Emanuel Berg
2014-09-10  0:49             ` Robert Thorpe
2014-09-10  1:21             ` Ken
2014-09-10  6:00               ` Glyn Millington
2014-09-10 14:31                 ` Ken
2014-09-10 13:12 ` Phillip Lord
2014-09-10 14:06   ` Thien-Thi Nguyen
2014-09-10 14:26   ` Stefan Monnier
2014-09-10 15:45     ` Phillip Lord
2014-09-10 16:59       ` Stefan Monnier
2014-09-11 11:10         ` Phillip Lord
2014-09-11 11:10         ` Phillip Lord
     [not found]   ` <mailman.8619.1410357764.1147.help-gnu-emacs@gnu.org>
2014-09-10 21:39     ` Emanuel Berg

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

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

  git send-email \
    --in-reply-to=87tzhkkuho.fsf@kobe.laptop \
    --to=keramida@ceid.upatras.gr \
    --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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.