From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Roland Winkler Newsgroups: gmane.emacs.help Subject: Re: replacing a certain element in a list with another Date: 23 Oct 2003 14:46:39 +0200 Organization: FAU Erlangen-Nuernberg Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: <86ekxcykfs.fsf@slowfox.dyndns.org> <9PBlb.171$lK3.18@news.level3.com> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1066914146 22204 80.91.224.253 (23 Oct 2003 13:02:26 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 23 Oct 2003 13:02:26 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Oct 23 15:02:24 2003 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1ACf6d-0000aP-00 for ; Thu, 23 Oct 2003 15:02:24 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.24) id 1ACf5f-0001y7-Cq for geh-help-gnu-emacs@m.gmane.org; Thu, 23 Oct 2003 09:01:23 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!bloom-beacon.mit.edu!npeer.de.kpn-eurorings.net!uni-erlangen.de!news.uni-erlangen.de!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 47 Original-NNTP-Posting-Host: tfkp07.physik.uni-erlangen.de Original-X-Trace: rznews2.rrze.uni-erlangen.de 1066913199 14827 131.188.164.207 (23 Oct 2003 12:46:39 GMT) Original-X-Complaints-To: news@uni-erlangen.de Original-NNTP-Posting-Date: 23 Oct 2003 12:46:39 GMT User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Original-Xref: shelby.stanford.edu gnu.emacs.help:117551 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.2 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:13483 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:13483 Barry Margolin writes: > When you start, you have the following: > > Cell A: car = 1 > cdr = reference to Cell B > Cell B: car = 2 > cdr = reference to Cell C > Cell C: car = 3 > cdr = reference to Cell D > Cell D: car = 4 > cdr = nil > x's value cell = reference to Cell A > > When you call nreverse, it is passed the contents of x's value cell, > i.e. the reference to Cell A. It reverses the list by keeping all the > car's the same, but reversing the cdr links; this changes things to: > > Cell A: car = 1 > cdr = nil > Cell B: car = 2 > cdr = reference to Cell A > Cell C: car = 3 > cdr = reference to Cell B > Cell D: car = 4 > cdr = reference to Cell C > > Now the first cons in the reversed list is Cell D, and nreverse returns > this as its value. > > But x's value cell still contains a reference to Cell A, which has become > the last cons in the chain. So when you print it, you just see the > 1-element list containing the last element of the reversed list. > > Why is it done this way? Because this is the simplest way to reverse a > list in place. It can just step through the list, replacing each cdr with > a reference to the preceding cons cell. This is much easier than swapping > all the car's in order to keep the order of the conses intact. Thank you very much! It might be helpful to add a comment to the docstring or info page for nreverse saying that its argument is modified such that afterwards it is a 1-element list containing the last element of the reversed list. Roland