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

Re: "ocaml_beginners"::[] Simple pointer arithmetic

Expand Messages
  • rixed@happyleptic.org
    -[ Wed, Feb 17, 2010 at 04:34:32PM +0100, Florent Monnier ]---- ... Naaaa, in Ocaml a thing is generaly not _from_ an array, but can be referenced by an
    Message 1 of 7 , Feb 17, 2010
    • 0 Attachment
      -[ Wed, Feb 17, 2010 at 04:34:32PM +0100, Florent Monnier ]----
      > Le mercredi 17 février 2010 14:52:44, vous avez écrit :
      > > Suppose I have an array of things, and a thing from this array.

      Naaaa, in Ocaml a "thing" is generaly not _from_ an array, but can
      be referenced by an array. So knowing the thing does not give
      information about where it is in the array. I still have difficulty
      to see an array as something else than a compact cluster of objects
      packed side to side. Too much C lately perhaps.

      > We can not talk about pointer arithmetic in OCaml, because OCaml does not
      > provide pointers. OCaml is a lot higher level than C.

      I feel both dumb and sorry for the time you spent explaining me
      my mistake. I will change my data structures so that I don't need
      to know this array index.

      Thank you.
    • Eric Cooper
      ... The idiomatic way to do this in OCaml is just to use the index of the thing, i.e., write a lookup function that searches (linearly) for the object and
      Message 2 of 7 , Feb 17, 2010
      • 0 Attachment
        On Wed, Feb 17, 2010 at 02:52:44PM +0100, rixed@... wrote:
        > Suppose I have an array of things, and a thing from this array.
        > I would like to now the location of the thing into the array.

        The idiomatic way to do this in OCaml is just to use the index of the
        thing, i.e., write a lookup function that searches (linearly) for the object
        and returns its index, or else raises Not_found.

        If this is too inefficient for your application, then you should use a
        different data structure, like a hash table or map, rather than an
        array.

        --
        Eric Cooper e c c @ c m u . e d u
      • Mac Mason
        ... It s worth pointing out that the algorithm for find the index of value X in array Y, or return a dummy value if doesn t exist is the same in both OCaml,
        Message 3 of 7 , Feb 17, 2010
        • 0 Attachment
          On Feb 17, 2010, at 10:50 AM, rixed@... wrote:
          > I feel both dumb and sorry for the time you spent explaining me > my
          > mistake. I will change my data structures so that I don't need > to
          > know this array index.

          It's worth pointing out that the algorithm for "find the index of value
          X in array Y, or return a dummy value if doesn't exist" is the same in
          both OCaml, and C, and every other language: examine each element of the
          array in turn, and return the index if it's it's equal to X. This can,
          of course, be done faster if the array is known to be sorted (binary
          search), and "is X in structure Y" can be done faster using a data
          structure that's actually designed to do it quickly, like Set.

          --Mac

          --
          Julian "Mac" Mason mac@... www.cs.duke.edu/~mac
        • rixed@happyleptic.org
          -[ Wed, Feb 17, 2010 at 12:01:19PM -0500, Mac Mason ]---- ... Except that in C I would have the address of Y, not only its value, so index will be (address of
          Message 4 of 7 , Feb 17, 2010
          • 0 Attachment
            -[ Wed, Feb 17, 2010 at 12:01:19PM -0500, Mac Mason ]----
            > On Feb 17, 2010, at 10:50 AM, rixed@... wrote:
            > It's worth pointing out that the algorithm for "find the index of value
            > X in array Y, or return a dummy value if doesn't exist" is the same in
            > both OCaml, and C, and every other language: examine each element of the
            > array in turn, and return the index if it's it's equal to X.

            Except that in C I would have the address of Y, not only its value,
            so index will be (address of Y - address of array) / size of a Y.
            That's what I was after.
          Your message has been successfully submitted and would be delivered to recipients shortly.