all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tassilo Horn <tassilo@member.fsf.org>
To: Daniel Schoepe <daniel@schoepe.org>
Cc: emacs-devel@gnu.org
Subject: Re: Side effects of `sort'
Date: Thu, 01 Mar 2012 21:52:56 +0100	[thread overview]
Message-ID: <87obsg3uxj.fsf@thinkpad.tsdh.de> (raw)
In-Reply-To: <8762eoum93.fsf@schoepe.localhost> (Daniel Schoepe's message of "Thu, 01 Mar 2012 20:58:16 +0100")

Daniel Schoepe <daniel@schoepe.org> writes:

Hi Daniel,

> according to describe-function, `sort' modifies its input list, but
> not in any way that the programmer can rely on. (For example, '(2 1 3)
> becomes '(2 3)). I assume the precise thing that ends up in the
> original list is an implementation detail and being able to "destroy"
> the original list has some performance benefits.
>
> Personally, I find this behavior very surprising and think it would make
> more sense to either set the input list to the final sorting result (in
> addition to returning it) or not to modify the input.

That's the point of all destructive operations like sort, nconc,
nreverse and so forth.  The perform better, because they don't copy but
only rearrange pointers in their input, and you must always use the
return value.

In your example, foo points to the list (2 1 3), i.e., foo is a pointer
to the cons cell (2 . cdr).

  foo -> (2 . (1 . (3 . nil)))

2 is bigger than 1, so we can make the 1-cons-cdr point to the 2-cons,
and the 2-cons-cdr point to the cdr of the 1-cons.

  (1 . (2 . (3 . nil)))
       ^
       |
      foo

However, the variable foo still points to the original cons cell (2
. cdr).  See?

Bye,
Tassilo



  reply	other threads:[~2012-03-01 20:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-01 19:58 Side effects of `sort' Daniel Schoepe
2012-03-01 20:52 ` Tassilo Horn [this message]
2012-03-01 21:04 ` Dave Abrahams
2012-03-01 21:34   ` Daniel Schoepe
2012-03-01 22:25   ` Stefan Monnier
2012-03-02  6:13     ` Miles Bader

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

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

  git send-email \
    --in-reply-to=87obsg3uxj.fsf@thinkpad.tsdh.de \
    --to=tassilo@member.fsf.org \
    --cc=daniel@schoepe.org \
    --cc=emacs-devel@gnu.org \
    /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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.