unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Drew Adams" <drew.adams@oracle.com>
To: "'Deniz Dogan'" <deniz.a.m.dogan@gmail.com>,
	"'David Kastrup'" <dak@gnu.org>
Cc: emacs-devel@gnu.org
Subject: RE: Why aren't there functions such as filter, take-while, etc. "by default"?
Date: Sun, 25 Apr 2010 15:56:15 -0700	[thread overview]
Message-ID: <90A0FD191C5F4D1A9F844EC734695D49@us.oracle.com> (raw)
In-Reply-To: <m2x7b501d5c1004251327ra860ebaep16844f4234d7306e@mail.gmail.com>

> `sort' which given a list and a predicate sorts the list
> destructively (rendering the original list useless) and then returns a
> sorted copy of the original list.

`sort' does not return a copy. It returns the sorted list, that is, the modified
list.

,----
| sort is a built-in function in `C source code'.
| 
| (sort LIST PREDICATE)
| 
| Sort LIST, stably, comparing elements using PREDICATE.
| Returns the sorted list.  LIST is modified by side effects.
          ^^^^^^^^^^^^^^^           ^^^^^^^^
| PREDICATE is called with two elements of LIST, and should return non-nil
| if the first element should sort before the second.
| 
| [back]
`----

> (let* ((my-list (list 3 2 1))
>        (sorted (sort my-list '<)))
>   my-list) ;; returns (3)

The sorted list is (1 2 3). If your sexp returned `sorted' instead of `my-list'
then that is what you would get.

Your sexp returns (3) because `my-list' points to the cons cell whose car is 3,
and after sorting that same cons cell is the last one in the sorted list. If
`sort' returned a complete copy, then its result (`sorted') would not share any
list structure with the original list. Try this, and you will see that `sorted'
is (1 2 5). The last cons cell in `sorted' is the cons cell pointed to by
`my-list'.

(let* ((my-list (list 3 2 1))
       (sorted (sort my-list '<)))
  (setcar my-list 5)
  (message "sorted: %S" sorted) (sit-for 3) ; (1 2 5)
  my-list) ; returns (5)





  reply	other threads:[~2010-04-25 22:56 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-24 20:13 Why aren't there functions such as filter, take-while, etc. "by default"? Deniz Dogan
2010-04-25 16:24 ` David Kastrup
2010-04-25 19:09   ` Štěpán Němec
2010-04-25 20:27   ` Deniz Dogan
2010-04-25 22:56     ` Drew Adams [this message]
2010-04-25 23:10       ` Deniz Dogan
2010-04-26  3:39   ` Stefan Monnier
2010-04-26  8:29     ` David Kastrup
2010-04-26  8:35       ` David Kastrup

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=90A0FD191C5F4D1A9F844EC734695D49@us.oracle.com \
    --to=drew.adams@oracle.com \
    --cc=dak@gnu.org \
    --cc=deniz.a.m.dogan@gmail.com \
    --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 public inbox

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

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).