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: sejourne_kevin <sejourne_kevin@y...>
Subject: Re: [Caml-list] Recursive lists
Jean-Christophe Filliatre wrote:
> 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.)
> 
With the modification of Alex Baretta this function can be be view as 
returning a _new_ list. So the list is immutable. When I right this 
function it was for _fun_. So if Obj don't exist I use a C primitive.

I can have a cycle using '[]' and '::' syntaxe this a great thing.
I think a improvement for Ocaml3.09 that allow
let ([]) = ...  and (::) = ...
is a better way and simpler.


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