From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id AAA55BC6B for ; Tue, 30 Oct 2007 02:38:41 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Aq4HABIoJkfQOfEbWmdsb2JhbACBWoZ6hgEBIA X-IronPort-AV: E=Sophos;i="4.21,345,1188770400"; d="scan'208";a="3811975" Received: from lgb-static-208.57.241.27.mpowercom.net (HELO sanddune.1969web.com) ([208.57.241.27]) by mail1-smtp-roc.national.inria.fr with ESMTP; 30 Oct 2007 02:38:41 +0100 Received: from [10.3.2.21] (zilles.1969web.com [10.3.2.21]) by sanddune.1969web.com (Postfix) with ESMTP id 59F7BBAD423; Mon, 29 Oct 2007 18:38:38 -0700 (PDT) Message-ID: <47268B10.8070407@1969web.com> Date: Mon, 29 Oct 2007 18:38:24 -0700 From: Karl Zilles Organization: 1969 Communications, Inc. User-Agent: Thunderbird 2.0.0.6 (Windows/20070728) MIME-Version: 1.0 To: Julien Moutinho Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Preferred Way to Split a List References: <47266DB7.1020009@SmokejumperIT.com> <20071030012012.GA29836@localhost> In-Reply-To: <20071030012012.GA29836@localhost> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; val:01 bool:01 val:01 non-mutable:01 ffht:98 hen:98 wrote:01 rec:01 caml-list:01 int:01 int:01 data:02 modifying:02 match:02 unit:03 Julien Moutinho wrote: > (* may trash the given list but tail-recursive *) > # let magic_split l f = > let rec aux p = > function > | [] -> l,[] > | h::t as b -> > if f h then aux (Some b) t > else match p with None -> [],l > | Some p -> (Obj.magic p).(1) <- None; l,b > in aux None l;; > val magic_split : 'a list -> ('a -> bool) -> 'a list * 'a list = > # let l = [1;2;3;4;5;6];; > val l : int list = [1; 2; 3; 4; 5; 6] > # magic_split l ((>=) 4);; > - : int list * int list = ([1; 2; 3; 4], [5; 6]) > # l;; > - : int list = [1; 2; 3; 4] Wow, this looks like a horrible idea! You're modifying a non-mutable data structure and using object magic. This leads to madness: # let test () = magic_split [1; 2; 3; 4; 5; 6] ((>=) 4);; val test : unit -> int list * int list = # test ();; - : int list * int list = ([1; 2; 3; 4], [5; 6]) # test ();; - : int list * int list = ([1; 2; 3; 4], []) I'm sure you're aware of this, but to suggest this as an answer to his question...