Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Clarification on some ocaml stuff
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-04-18 (03:25)
From: Ryan Bastic <rbastic@g...>
Subject: [Caml-list] Clarification on some ocaml stuff
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 []
      let (front, rest) = firstN n input in
        (* the next line confuses me. i'm aware of :: being a list
           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
    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,

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: