Loading ...
Sorry, an error occurred while loading the content.
 

Beginner: problems creating a method to manipulate array

Expand Messages
  • k359525
    Hi. I m a University student who s relatively new to the Ocaml language, and I m having some problems with array manipulation on a program I m writing. I d be
    Message 1 of 4 , Mar 31, 2005
      Hi. I'm a University student who's relatively new to the Ocaml
      language, and I'm having some problems with array manipulation on a
      program I'm writing. I'd be grateful if anyone could help me out.
      In a class, mynode, I've created a new array called "ft" :
      val ft = Array.make 100 (None)
      I'm not entirely sure what the (None) part is meant to be for, but if
      it's not there Ocaml doesn't seem to want to compile it. First
      question: What is it there for and why do I need it?
      Second, I have a method for adding an entry to this array, which I've
      written like this:
      (* Add a forwarding table entry at <nodenum> *)
      method addftentry nodenum entry =
      ft.(nodenum) <- entry
      and the .mli code for this part looks like this:
      method addftentry : nodenum:int -> entry:int -> unit
      However, trying to compile this gives me the error:
      This expression has type unit but is here used with type int
      Can anyone help me out with this and tell me where I'm going wrong please?
      Thanks.
    • Jon Harrop
      ... If you re new to OCaml then I d recommend postponing learning about objects. :-) ... The array elements must be initialised to some value. What you have
      Message 2 of 4 , Mar 31, 2005
        On Friday 01 April 2005 02:16, k359525 wrote:
        > Hi. I'm a University student who's relatively new to the Ocaml
        > language, and I'm having some problems with array manipulation on a
        > program I'm writing. I'd be grateful if anyone could help me out.
        > In a class, mynode,

        If you're new to OCaml then I'd recommend postponing learning about
        objects. :-)

        > I've created a new array called "ft" :
        > val ft = Array.make 100 (None)
        > I'm not entirely sure what the (None) part is meant to be for, but if
        > it's not there Ocaml doesn't seem to want to compile it. First
        > question: What is it there for and why do I need it?

        The array elements must be initialised to some value. What you have done
        initialises them to an optional value. The type of the resulting array
        requires some more advanced knowledge so I won't describe it here.

        > Second, I have a method for adding an entry to this array, which I've
        > written like this:
        > (* Add a forwarding table entry at <nodenum> *)
        > method addftentry nodenum entry =
        > ft.(nodenum) <- entry

        Ok, so the type of an element in the array "ft" must be the same as the type
        of the second function argument "entry".

        > and the .mli code for this part looks like this:
        > method addftentry : nodenum:int -> entry:int -> unit

        This type is actually for labelled arguments. For now, just forget about
        labelled arguments, so the type you want is written:

        method addftentry : int -> int -> unit

        So you'll want to have initialised your arrays to zeros:

        val ft = Array.make 100 0

        > However, trying to compile this gives me the error:
        > This expression has type unit but is here used with type int
        > Can anyone help me out with this and tell me where I'm going wrong please?

        You are actually using some quite advanced features of OCaml. Unlike C++ and
        Java, OCaml objects are rarely needed and should be used rarely in practice.
        Labelled arguments are often very useful but you don't have to use them so
        I'd leave them till later too. By using "None" you've dragged in
        monomorphism... :-)

        I recommend you read Richard Jones' excellent tutorial and the freely
        available first chapter of my book, both linked to from:

        http://www.ffconsultancy.com/products/ocaml_for_scientists

        HTH.

        --
        Dr Jon D Harrop, Flying Frog Consultancy Ltd.
        http://www.ffconsultancy.com
      • Karl Zilles
        ... This is a good point. Unless you re specifically required to write an object, then this is not the way to start. If it s just a java/c++ mentality, then
        Message 3 of 4 , Mar 31, 2005
          Jon Harrop wrote:
          > On Friday 01 April 2005 02:16, k359525 wrote:
          >
          >>Hi. I'm a University student who's relatively new to the Ocaml
          >>language, and I'm having some problems with array manipulation on a
          >>program I'm writing. I'd be grateful if anyone could help me out.
          >>In a class, mynode,
          >
          >
          > If you're new to OCaml then I'd recommend postponing learning about
          > objects. :-)

          This is a good point. Unless you're specifically required to write an
          object, then this is not the way to start. If it's just a java/c++
          mentality, then shake that off.

          ...

          >
          >>and the .mli code for this part looks like this:
          >> method addftentry : nodenum:int -> entry:int -> unit

          You don't actually have to write a .mli file for your code until you
          want to start hiding things (abstractions). It's much easier at your
          level to omit it.

          Also, if you want the compiler to generate an .mli file, you can run it
          with the -i option on your ml file.
        • k359525
          ... The main reasons I m approaching the problem using objects is firstly because I have a background in Java, and therefore object-oriented programming is a
          Message 4 of 4 , Apr 1, 2005
            --- In ocaml_beginners@yahoogroups.com, Karl Zilles <zilles@1...> wrote:
            > Jon Harrop wrote:
            > > On Friday 01 April 2005 02:16, k359525 wrote:
            > >
            > >>Hi. I'm a University student who's relatively new to the Ocaml
            > >>language, and I'm having some problems with array manipulation on a
            > >>program I'm writing. I'd be grateful if anyone could help me out.
            > >>In a class, mynode,
            > >
            > >
            > > If you're new to OCaml then I'd recommend postponing learning about
            > > objects. :-)
            >
            > This is a good point. Unless you're specifically required to write an
            > object, then this is not the way to start. If it's just a java/c++
            > mentality, then shake that off.
            >

            The main reasons I'm approaching the problem using objects is firstly
            because I have a background in Java, and therefore object-oriented
            programming is a more familiar concept to me, which I guess I tend to
            adhere to. Secondly, and more importantly, I'm using the network
            simulator NAB, which is written in Ocaml, and I'm required to edit
            objects already written (I guess I could rewrite them myself without
            using objects, but I figured that'll be more hassle than I need).
            Anyway, thank you very much for your help. I really do appreciate it.
          Your message has been successfully submitted and would be delivered to recipients shortly.