* The kth element
@ 2007-01-15 21:52 Lloyd Moore
2007-01-15 22:25 ` [Caml-list] " Jonathan Bryant
0 siblings, 1 reply; 2+ messages in thread
From: Lloyd Moore @ 2007-01-15 21:52 UTC (permalink / raw)
To: caml-list
[-- Attachment #1: Type: text/plain, Size: 276 bytes --]
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) ;;
[-- Attachment #2: Type: text/html, Size: 838 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] The kth element
2007-01-15 21:52 The kth element Lloyd Moore
@ 2007-01-15 22:25 ` Jonathan Bryant
0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Bryant @ 2007-01-15 22:25 UTC (permalink / raw)
To: Lloyd Moore; +Cc: caml-list
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2007-01-15 22:26 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-15 21:52 The kth element Lloyd Moore
2007-01-15 22:25 ` [Caml-list] " Jonathan Bryant
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox