Version française
Home     About     Download     Resources     Contact us    
Browse thread
The kth element
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jonathan Bryant <jtbryant@v...>
Subject: Re: [Caml-list] The kth element

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