Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Re: [Caml-list] time complexity on basic data types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-08-24 (12:28)
From: Jean-Christophe Filliatre <Jean-Christophe.Filliatre@l...>
Subject: Re: [Caml-list] time complexity on basic data types

Lars Nilsson writes:
 > At the risk of making a fool of myself in public, is there such a thing as
 > insertion in a list at all in Ocaml? From what I have seen there is only
 > concatenation of a single element and a list (::), and operations would have
 > to be defined with this by means recursion/iteration. If this is the case, I
 > assume insertion at some point in a list would have O(n) complexity in the
 > general case? If not, what am I missing (@ being something other than I
 > think?)

You're right. But a list is not the adequate data structure if you want
to insert at some arbitrary points in it. It is a stack-like data
structure (i.e. push and pop are O(1)) or can be used when you want to
aggregate elements regardless the order and then iterate over / traverse
all of them. 

If you really want to insert at any point in O(1), you may consider using
mutable linked lists (see for instance the implementation of the
module Queue in ocaml standard library). You loose persistence, but it
may not be mandatory in your case.

Hope this helps,
Jean-Christophe Filliatre
Bug reports:  FAQ:
To unsubscribe, mail  Archives: