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
[Caml-list] Recursive lists
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jean-Christophe Filliatre <Jean-Christophe.Filliatre@l...>
Subject: Re: [Caml-list] Recursive lists

sejourne_kevin wrote:
> (** Take a list and connect the end on the beginning
>    Copyright : Kévin ;)
> *)
> let cycle l =
>   let rl= ref l in
>   let rec go_fin = function
>       [] -> invalid_arg "cycle:[] can't be !"
>     | [x] as f -> Obj.set_field (Obj.repr f) 1 (Obj.repr !rl);l
>     | x::reste-> go_fin reste
>   in go_fin l
> ;;
> I haven't test GC issu.

This shouldn't be advised, and not even posted on this list.

This main property of Ocaml's lists is to be _immutable_ and therefore
to  implement a  _persistent_ data  type. This  is full  of  very nice
consequences  for the  programmer.  (Read  Okasaki's book  or previous
posts on this list explaining what persistence is.)

But  if you  start mutating  lists, you  break this  property  and you
endanger your code.  If you need to mutate lists, why  don't you use a
mutable  data  structure,  such  as regular  (mutable)  chained  lists
exactly as  in traditional  imperative programming? (See  for instance
Ocaml's Queue implementation.)

Jean-Christophe Filliâtre (


If you read French, I have an ocaml tutorial on my web page I recently
wrote for students,  where a chapter is dedicated  to persistence: see

This  is a  rather modest  contribution (compared  to  Richard Jones's
tutorial for instance)  and it does not even  describe all features of
ocaml, but at least it explains why you shouldn't mutate lists.

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: