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=1.0 required=5.0 tests=AWL,SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id CFD39BC6B for ; Tue, 30 Oct 2007 02:20:12 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAOoiJkfRVYa8kmdsb2JhbACOXwEBAQEHBAQTFg X-IronPort-AV: E=Sophos;i="4.21,345,1188770400"; d="scan'208";a="18748094" Received: from mu-out-0910.google.com ([209.85.134.188]) by mail4-smtp-sop.national.inria.fr with ESMTP; 30 Oct 2007 02:20:12 +0100 Received: by mu-out-0910.google.com with SMTP id w8so2483120mue for ; Mon, 29 Oct 2007 18:20:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:received:date:to:subject:message-id:references:mime-version:content-type:content-disposition:in-reply-to:user-agent:from; bh=nQlo9tNxjvLZmxZ8jO11hTojOeNsuLA77tyDydKlO1k=; b=tm5fzI1BWZd2CIrjRCjubyVZMJ2blm31mgicdFPRdf66+ceuZZ9Zjlym8Gops6GbS6EKIYHAf78+GrGlOzyLb7VzDp8/MuU/EAo5wu1A5mr7udLNqcOQTp0MwI17e2e0PRARAxwVBgxEc9WP4wovEdyx5FoW8Acljg0wt/xC88A= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:date:to:subject:message-id:references:mime-version:content-type:content-disposition:in-reply-to:user-agent:from; b=Bm0aqZpTSA8bR/qtDkGPgGALe5WK11RXvvV+nLKc+j2523B8L7D9HyKcWSAwn0vNMEJTSw3D8aC2a/x61kA4TrTrHnyF8JuQxqVMIrN0oYOLaEsrq5YX4VrF2UuivUautpc6F1yAq/jgeuLxpYp1GjT9fO3+CPIBa+BKrWafeIY= Received: by 10.86.81.8 with SMTP id e8mr5240357fgb.1193707211381; Mon, 29 Oct 2007 18:20:11 -0700 (PDT) Received: from localhost ( [83.201.31.218]) by mx.google.com with ESMTPS id k7sm7311897nfh.2007.10.29.18.19.54 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 29 Oct 2007 18:20:09 -0700 (PDT) Received: by localhost (sSMTP sendmail emulation); Tue, 30 Oct 2007 02:20:13 +0100 Date: Tue, 30 Oct 2007 02:20:13 +0100 To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Preferred Way to Split a List Message-ID: <20071030012012.GA29836@localhost> References: <47266DB7.1020009@SmokejumperIT.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <47266DB7.1020009@SmokejumperIT.com> User-Agent: Mutt/1.5.16 (2007-06-11) From: Julien Moutinho X-Spam: no; 0.00; val:01 bool:01 val:01 bool:01 ffht:98 hen:98 ffht:98 hen:98 wrote:01 rec:01 rec:01 caml-list:01 int:01 int:01 arbitrary:02 On Mon, Oct 29, 2007 at 06:33:11PM -0500, Robert Fischer wrote: > What is the preferred way to split a list into two, at an arbitrary point? > There's lots of ways you could do it, but I'm not sure if there's a > standard best practice for this. Two answers for what they're worth: # let split l f = let rec aux = function | h::t as l -> if f h then let a,b = aux t in h::a,b else [],l | [] -> [],[] in aux l;; val split : 'a list -> ('a -> bool) -> 'a list * 'a list = # split [1;2;3;4;5;6] ((>=) 4);; - : int list * int list = ([1; 2; 3; 4], [5; 6]) (* 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] HTH, Julien.