caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Markus Mottl <markus@oefai.at>
To: Brian Hurt <brian.hurt@qlogic.com>
Cc: Ocaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Probably FAQ: Why is list-append (list :: elem) so expensive?
Date: Wed, 25 Sep 2002 18:17:03 +0200	[thread overview]
Message-ID: <20020925161703.GC31883@fichte.ai.univie.ac.at> (raw)
In-Reply-To: <Pine.LNX.4.33.0209250958520.1974-100000@eagle.ancor.com>

On Wed, 25 Sep 2002, Brian Hurt wrote:
> I get that the cost of list concatenation is proportional to the length of 
> the first list.  So (elem :: list) is O(1) no matter what the length of 
> list is.  But (list :: elem) is O(n), with n being the length of the list.

You probably mean (list @ [elem]).

> Why doesn't Ocaml keep a pointer to the last element of the list,
> as well as the first element?

Because standard lists are purely functional, i.e. you can't just
overwrite some pointer to append an element. If there are several lists
that share a common sublist, such an operation would change both lists.

E.g.:

  let l = [1; 2; 3] in
  let l1 = 0 :: l in
  let l2 = 42 :: l in
  l @ [4]

If the last operation overwrote anything in "l", this would also change
l1 and l2, thus destroying referential transparency: list variables
couldn't be used for straightforward equational reasoning anymore,
which is important if you want to e.g. proof list algorithms correct.

As you noted, keeping pointers to the previous element also makes
list nodes larger and thus more expensive both in terms of time and
space. Why have this by default if lists are most often not used in ways
that require efficiency with such operations?

If you want to have mutable lists, it's not difficult to implement them.
But be warned: you might be surprised to discover that standard lists
most often perform better in not too extreme cases.

In case you want to learn more about purely functional datastructures,
you will definitely want to read Chris Okasaki's book "Purely Functional
Data Structures", which is published by Cambridge University Press (1998).

Regards,
Markus Mottl

-- 
Markus Mottl                                             markus@oefai.at
Austrian Research Institute
for Artificial Intelligence                  http://www.oefai.at/~markus
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  parent reply	other threads:[~2002-09-25 16:17 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-09-25 15:33 Brian Hurt
2002-09-25 15:39 ` Noel Welsh
2002-09-25 15:42 ` Oleg
2002-09-25 16:17 ` Markus Mottl [this message]
2002-09-25 18:44   ` Brian Hurt
2002-09-25 19:22     ` Markus Mottl
2002-09-26  7:10     ` Florian Hars
2002-09-26 14:44 ` Kontra, Gergely

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=20020925161703.GC31883@fichte.ai.univie.ac.at \
    --to=markus@oefai.at \
    --cc=brian.hurt@qlogic.com \
    --cc=caml-list@inria.fr \
    /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 NNTP newsgroup(s).