From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Giorgos Keramidas Newsgroups: gmane.emacs.help Subject: Re: Basic Emacs Lisp question Date: Wed, 30 Apr 2008 04:21:39 +0300 Organization: SunSITE.dk - Supporting Open source Message-ID: <87tzhkkuho.fsf@kobe.laptop> References: <67p1csF2qe3gfU1@mid.dfncis.de> <87od7sa6ke.fsf@kobe.laptop> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1209519648 5170 80.91.229.12 (30 Apr 2008 01:40:48 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 30 Apr 2008 01:40:48 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Apr 30 03:41:25 2008 connect(): Connection refused Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1Jr1Jx-0007zy-Ae for geh-help-gnu-emacs@m.gmane.org; Wed, 30 Apr 2008 03:41:21 +0200 Original-Received: from localhost ([127.0.0.1]:58459 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Jr1JG-0007vD-Jr for geh-help-gnu-emacs@m.gmane.org; Tue, 29 Apr 2008 21:40:38 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!feed118.news.tele.dk!dotsrc.org!filter.dotsrc.org!news.dotsrc.org!not-for-mail Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (berkeley-unix) Cancel-Lock: sha1:ofTNV3E+c/rVoHU0RWVej0Q1BMA= Original-Lines: 115 Original-NNTP-Posting-Host: 77.49.228.110 Original-X-Trace: news.sunsite.dk DXC=lXQ`3U2L2m:=M\fUi`VZVL^Mj7bH?lJH1Tkj6 Original-X-Complaints-To: staff@sunsite.dk Original-Xref: shelby.stanford.edu gnu.emacs.help:158271 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:53637 Archived-At: On Tue, 29 Apr 2008 20:56:17 +0300, Giorgos Keramidas wrote: > On Tue, 29 Apr 2008 17:49:19 +0200, Matthias Pfeifer 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