Version française
Home     About     Download     Resources     Contact us    
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 (http://www.lri.fr/~filliatr)

PS: 

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
http://www.lri.fr/~filliatr/publis/ipf.ps.gz

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