caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Ryan Bastic" <rbastic@gis.net>
To: caml-list@inria.fr
Subject: [Caml-list] Clarification on some ocaml stuff
Date: Thu, 17 Apr 2003 23:25:19 -0400	[thread overview]
Message-ID: <3e9f701f.1227.4928@gis.net> (raw)

Hello all,
  A few weeks ago I posted a message asking how to create a program in
OCaml to shuffle  vector of strings into an array of arrays. Had some
problems understanding how to do it in Ocaml properly, and a kind soul
posted a very elegant solution to the problem :-) unfortunately, i still
can't understand some things from it.

  What follows is some code with comments on how it is expected to be
used, and also
where my confusion in the semantics of the code lay.

  (* Return first 'n' from input and the rest. *)
let firstN n input =
  let nInput = Array.length input in
    if n >= nInput
    then (input, [||])
    else (Array.sub input 0 n, Array.sub input n (nInput - n))

let group n input =
  let rec group' n input =
    if Array.length input = 0 then []
    else 
      let (front, rest) = firstN n input in
        (* the next line confuses me. i'm aware of :: being a list
concatenation
           operator, but in this case, shouldn't group 'n rest be
returning an array,
           because that's what firstN returns. I've experimented in the
REPL and had
           no luck in figuring it out. *)
    	front :: group' n rest
  in
    Array.of_list (group' n input)

if you pop this into the toplevel, and do:

group 2 [|"Name1"; "Name2"; "Name3"; "Name4"; "Name5";|];;

you'll see the general functionality expected.

thanks for any help,
-Ryan
http://malander.undrgnd.net

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


             reply	other threads:[~2003-04-18  3:25 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-18  3:25 Ryan Bastic [this message]
2003-04-18  4:09 ` SooHyoung Oh

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=3e9f701f.1227.4928@gis.net \
    --to=rbastic@gis.net \
    --cc=caml-list@inria.fr \
    /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).