From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Barry Margolin Newsgroups: gmane.emacs.help Subject: Re: replacing a certain element in a list with another Date: Wed, 22 Oct 2003 20:32:05 GMT Organization: Level(3) Communications, Woburn, MA Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: <9PBlb.171$lK3.18@news.level3.com> References: <86ekxcykfs.fsf@slowfox.dyndns.org> NNTP-Posting-Host: deer.gmane.org X-Trace: sea.gmane.org 1066856523 26272 80.91.224.253 (22 Oct 2003 21:02:03 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Wed, 22 Oct 2003 21:02:03 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Oct 22 23:01:59 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 1ACQ7C-0008D4-00 for ; Wed, 22 Oct 2003 23:01:58 +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 1ACQ6q-0000V9-9M for geh-help-gnu-emacs@m.gmane.org; Wed, 22 Oct 2003 17:01:36 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!bloom-beacon.mit.edu!nycmny1-snh1.gtei.net!crtntx1-snh1.gtei.net!news.gtei.net!news.level3.com.POSTED!53ab2750!not-for-mail Original-Newsgroups: gnu.emacs.help Mail-Copies-To: never X-Newsreader: trn 4.0-test72 (19 April 1999) Originator: barmar@genuity.net (Barry Margolin) Original-Lines: 68 Original-NNTP-Posting-Host: 171.78.176.30 Original-X-Trace: news.level3.com 1066854725 171.78.176.30 (Wed, 22 Oct 2003 20:32:05 GMT) Original-NNTP-Posting-Date: Wed, 22 Oct 2003 20:32:05 GMT Original-Xref: shelby.stanford.edu gnu.emacs.help:117542 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:13473 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:13473 In article , Roland Winkler wrote: >Kai Grossjohann writes: >> Well, (nreverse x) does destructively modify the list x, but >> afterwards the value if x is not what you think: >> >> *** Welcome to IELM *** Type (describe-mode) for help. >> ELISP> (setq x (list 1 2 3 4)) >> (1 2 3 4) >> >> ELISP> (nreverse x) >> (4 3 2 1) >> >> ELISP> x >> (1) >> >> ELISP> >> >> Clear? > >...not really :-) > >I tried to understand the docstring and the info page. However, I >could not really make use of it. So what is it that nreverse is >doing with its argument? And when is this useful?? 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. -- Barry Margolin, barry.margolin@level3.com Level(3), Woburn, MA *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups. Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.