From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tim X Newsgroups: gmane.emacs.help Subject: Re: Basic Emacs Lisp question Date: Wed, 30 Apr 2008 18:37:30 +1000 Organization: Rapt Technologies Message-ID: <873ap3eo1h.fsf@lion.rapttech.com.au> References: <67p1csF2qe3gfU1@mid.dfncis.de> <87od7sa6ke.fsf@kobe.laptop> <87tzhkkuho.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 1209545014 5062 80.91.229.12 (30 Apr 2008 08:43:34 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 30 Apr 2008 08:43:34 +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 10:44:10 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 1Jr7uW-0000jM-TH for geh-help-gnu-emacs@m.gmane.org; Wed, 30 Apr 2008 10:43:33 +0200 Original-Received: from localhost ([127.0.0.1]:60931 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Jr7tq-0004cM-3n for geh-help-gnu-emacs@m.gmane.org; Wed, 30 Apr 2008 04:42:50 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!postnews.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local02.nntp.dca.giganews.com!nntp.internode!news.internode.POSTED!not-for-mail Original-NNTP-Posting-Date: Wed, 30 Apr 2008 03:37:31 -0500 Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux) Cancel-Lock: sha1:DVdkD3zGiOYu6034X22E83G31JQ= Original-Lines: 127 X-Usenet-Provider: http://www.giganews.com Original-NNTP-Posting-Host: 121.44.88.177 Original-X-Trace: sv3-sKtWcmqECHUDEPGNX3vkO9hDc4OGTyzVAYPBdPkzeJMCSvOQgu/HqFr9TAVgU3ii2t5Z5F3XZicpPXa!kBY7qoU0SnOiR4PiE3Puuf5L0gOB7L7Hm3Cu3SQa4gXw7E2RacKH4Cday7kCMRxDK2kNyrKqeg== Original-X-Complaints-To: abuse@internode X-DMCA-Complaints-To: abuse@internode X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.38 Original-Xref: shelby.stanford.edu gnu.emacs.help:158281 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:53647 Archived-At: Giorgos Keramidas writes: > 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 :-) > I think you expressed the difference quite clearly. In another post, I tried to point out a possible trap that many fall into - attempting to modify the list returned by the quoted version. This is why I tend to think of '(a b c) more like a constant, while (list a b c) is able to be modified with expected results. Tim -- tcross (at) rapttech dot com dot au