[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Basic Emacs Lisp question
From: |
Giorgos Keramidas |
Subject: |
Re: Basic Emacs Lisp question |
Date: |
Wed, 30 Apr 2008 04:21:39 +0300 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (berkeley-unix) |
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