unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Barry Margolin <barry.margolin@level3.com>
Subject: Re: replacing a certain element in a list with another
Date: Wed, 22 Oct 2003 20:32:05 GMT	[thread overview]
Message-ID: <9PBlb.171$lK3.18@news.level3.com> (raw)
In-Reply-To: m3k76xf34y.fsf@tfkp07.physik.uni-erlangen.de

In article <m3k76xf34y.fsf@tfkp07.physik.uni-erlangen.de>,
Roland Winkler  <Roland.Winkler@physik.uni-erlangen.de> wrote:
>Kai Grossjohann <kai.grossjohann@gmx.net> 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.

  reply	other threads:[~2003-10-22 20:32 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-09-30  8:38 replacing a certain element in a list with another Klaus Berndl
2003-09-30  9:05 ` David Kastrup
2003-09-30  9:59   ` Klaus Berndl
2003-09-30 10:03     ` David Kastrup
2003-09-30 10:19       ` Klaus Berndl
2003-09-30 12:12     ` Pascal Bourguignon
2003-09-30 14:14       ` Stefan Monnier
2003-09-30 14:26         ` Klaus Berndl
2003-09-30 14:16 ` Stefan Monnier
2003-09-30 14:28   ` Klaus Berndl
2003-09-30 15:03     ` Stefan Monnier
2003-09-30 15:30       ` Kevin Rodgers
2003-09-30 15:47         ` Stefan Monnier
2003-09-30 18:27           ` Roland Winkler
2003-09-30 20:01             ` Barry Margolin
2003-10-16 20:52             ` Kai Grossjohann
2003-10-22 20:06               ` Roland Winkler
2003-10-22 20:32                 ` Barry Margolin [this message]
2003-10-23 12:46                   ` Roland Winkler
2003-10-23 14:25                     ` Stefan Monnier
2003-10-23 15:30                       ` Kevin Rodgers
2003-10-23 15:18                     ` Barry Margolin
2003-10-23 22:03                       ` Roland Winkler
2003-10-23 22:20                         ` Barry Margolin
2003-10-24 12:02                           ` Roland Winkler
2003-10-24 15:06                             ` Barry Margolin
2003-10-25 11:25                             ` Oliver Scholz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='9PBlb.171$lK3.18@news.level3.com' \
    --to=barry.margolin@level3.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).