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=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id E8067BC0B for ; Mon, 15 Jan 2007 23:26:17 +0100 (CET) Received: from sccmmhc91.asp.att.net (sccmmhc91.asp.att.net [204.127.203.211]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l0FMQHYR014858 for ; Mon, 15 Jan 2007 23:26:17 +0100 Received: from [192.168.0.104] (12-208-241-105.client.mchsi.com[12.208.241.105]) by sccmmhc91.asp.att.net (sccmmhc91) with SMTP id <20070115222605m9100ns7ere>; Mon, 15 Jan 2007 22:26:15 +0000 In-Reply-To: <015f01c738ef$66bc63b0$6401a8c0@lloyd> References: <015f01c738ef$66bc63b0$6401a8c0@lloyd> Mime-Version: 1.0 (Apple Message framework v752.3) X-Priority: 3 Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: Cc: caml-list@yquem.inria.fr Content-Transfer-Encoding: 7bit From: Jonathan Bryant Subject: Re: [Caml-list] The kth element Date: Mon, 15 Jan 2007 17:25:30 -0500 To: Lloyd Moore X-Mailer: Apple Mail (2.752.3) X-Miltered: at discorde with ID 45ABFF89.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; argv:01 printf:01 printf:01 sml:01 sml:01 beginner's:01 ocaml:01 bug:01 2007,:98 beginners:01 wrote:01 rec:01 rec:01 caml-list:01 caml-list:01 On Jan 15, 2007, at 4:52 PM, Lloyd Moore wrote: or (with a test program): let kth_smallest k l = let rec insert n l x a = match (n,l) with | (0, _) -> List.rev a | (n, h::t) -> if x < h then insert (n - 1) t h (x::a) else insert (n - 1) t x (h::a) | (n, []) -> List.rev (x::a) in let l = List.fold_left (fun a x -> insert k a x []) [] l in List.hd (List.rev l) let _ = let x = kth_smallest (int_of_string Sys.argv.(1)) [1; 11; 5; 9; 7; 3; 13] in Printf.printf "%d\n" x > If I understood the question correctly posed by Dr Jon Harrop... > > let smallest lst = let rec sml lst v = > match lst with > [] -> v > | h::t -> > match t with > h::t -> if h < (List.hd t) then sml t h else sml t v > | _ -> v > in sml lst (List.hd lst) ;; > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs