From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id UAA29898; Fri, 8 Oct 2004 20:26:52 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id UAA29887 for ; Fri, 8 Oct 2004 20:26:50 +0200 (MET DST) Received: from alex.barettalocal.com (h213-255-109-130.albacom.net [213.255.109.130] (may be forged)) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id i98IQoDp027702 for ; Fri, 8 Oct 2004 20:26:50 +0200 Received: from [127.0.0.1] (localhost.localdomain [127.0.0.1]) by alex.barettalocal.com (Postfix) with ESMTP id ABD7DF386A; Fri, 8 Oct 2004 20:28:18 +0200 (CEST) Message-ID: <4166DC42.3090602@baretta.com> Date: Fri, 08 Oct 2004 20:28:18 +0200 From: Alex Baretta User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.3) Gecko/20040913 X-Accept-Language: it, en-us, en MIME-Version: 1.0 To: sejourne_kevin Cc: Luca Pascali , caml-list@inria.fr Subject: Re: [Caml-list] Recursive lists References: <41669437.3010201@yahoo.it> <4166A395.70301@yahoo.fr> In-Reply-To: <4166A395.70301@yahoo.fr> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Miltered: at concorde with ID 4166DBEA.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; baretta:01 baretta:01 caml-list:01 type-safe:01 implemented:01 aliasing:01 compiler:01 ocaml:01 alex:01 alex:01 rec:01 rec:01 wrote:03 recursive:03 recursive:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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. > Nice code. So clean. So idiomatic. So type-safe... Well, it's really this Obj stuff is the best we can do with the current intuition of recursive values implemented in the Ocaml compiler. Let me suggest a slightly safer version: let cycle l = let l = List.rev (List.rev l) in 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); | x::reste-> go_fin reste in go_fin l This first duplicates the original list so that aliasing is not an issue. BTW, I've just discovered that List.append is safe with respect to an infinite second argurment, which is a totally desirable property. It is open to discussion as to whether append should analyze l1 for cyclicity Alex ------------------- 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