caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jonathan Bryant <jtbryant@valdosta.edu>
To: Lloyd Moore <lloyd@encantado.net>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] The kth element
Date: Mon, 15 Jan 2007 17:25:30 -0500	[thread overview]
Message-ID: <AE025A67-7FBE-400A-8CA2-C2E61B883276@valdosta.edu> (raw)
In-Reply-To: <015f01c738ef$66bc63b0$6401a8c0@lloyd>


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


      reply	other threads:[~2007-01-15 22:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-01-15 21:52 Lloyd Moore
2007-01-15 22:25 ` Jonathan Bryant [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AE025A67-7FBE-400A-8CA2-C2E61B883276@valdosta.edu \
    --to=jtbryant@valdosta.edu \
    --cc=caml-list@yquem.inria.fr \
    --cc=lloyd@encantado.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).