Re: reference initialization

From: Daniel de Rauglaudre (daniel.de_rauglaudre@inria.fr)
Date: Sat May 13 2000 - 09:07:26 MET DST

  • Next message: Daniel de Rauglaudre: "Re: reference initialization"

    On Thu, May 11, 2000 at 02:59:16PM -0400, Hongwei Xi wrote:

    > I want to combine two arrays into one. Here is the code in OCaml.
    > ...

    Simple (library) solution:
    =======================
    let combine_arrays a b =
      let alen = Array.length a in
      let blen = Array.length b in
      Array.init (alen + blen)
        (fun i -> if i < alen then a.(i) else b.(i - alen))
    =======================

    Simple (custom) solution:
    =======================
    let combine_arrays a b =
      let alen = Array.length a in
      let blen = Array.length b in
      if alen = 0 && blen = 0 then [| |]
      else
        let c = Array.make (alen + blen) (if alen = 0 then b.(0) else a.(0)) in
        in begin
          for i = 0 to alen - 1 do
            c.(i) <- a.(i)
          done;
          for i = 0 to blen -1 do
            c.(alen + i) <- b.(i)
          done
        end
    =======================

    Dirty solution (don't tell them I told you):
    =======================
    let combine_arrays a b =
      let alen = Array.length a in
      let blen = Array.length b in
      let c = Array.make (alen + blen) (Obj.magic 0)
      in begin
        for i = 0 to alen - 1 do
          c.(i) <- a.(i)
        done;
        for i = 0 to blen -1 do
          c.(alen + i) <- b.(i)
        done
      end
    =======================

    -- 
    Daniel de RAUGLAUDRE
    daniel.de_rauglaudre@inria.fr
    http://cristal.inria.fr/~ddr/
    



    This archive was generated by hypermail 2b29 : Sun May 14 2000 - 23:19:26 MET DST