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

Re: "ocaml_beginners"::[] Copy a text file?

Expand Messages
  • Remi VANICAT
    ... String.create come from the standard library. Not the core one, but the standard one. You don t have to do anything different for compiling a program which
    Message 1 of 13 , Feb 8, 2002
    View Source
    • 0 Attachment
      stalkern2 <stalkern2@...> writes:

      > Yes! what you sent is straigth and simple (and gives 0o664
      > permissions) and I don't mind that String.create comes from outside
      > the standard library because
      > I can rewrite it as

      String.create come from the standard library. Not the core one, but
      the standard one. You don't have to do anything different for
      compiling a program which use any module from the standard library
      than for a program which don't use it, so why bother ?

      >
      > let string_create pattern size =
      > let rec str_cr pattern str size counter =
      > if counter < size
      > then str_cr pattern (str^pattern) size (counter +1)
      > else str
      > in
      > str_cr pattern "" size 0;;
      >
      > (BTW This shouldn't be very performant, fetching arguments around over and
      > over should take more memory than marking imperatively the value of a
      > counter... I suppose)

      you're wrong, you function is tail recursive (it don't do anything
      after calling itself), So it don't use the stack.

      the real problem is that each time you do a st1 ^ st2 you copy
      (length st1) + (length st2) byte in memory. so if I call

      string_create "a" 100 it will make (100)*(100 + 1)/2 = 5000 copy of
      one byte. String.create 100 will only allocate the 100 byte, it far
      (very far) more effective, and in the standard library.


      --
      Rémi Vanicat
      vanicat@labri.u-bordeaux.fr
      http://dept-info.labri.u-bordeaux.fr/~vanicat
    • Remi VANICAT
      ... no, it is a primitive (written in C). You can t make this kind of allocation explicitly in ocaml. (and you don t have to). -- Rémi Vanicat
      Message 2 of 13 , Feb 8, 2002
      View Source
      • 0 Attachment
        stalkern2 <stalkern2@...> writes:

        > Alle ore 06:09, venerdì 08 febbraio 2002, Remi VANICAT ha scritto:
        > > String.create 100 will only allocate the 100 byte, it far
        > > (very far) more effective
        >
        > Uhm! Is String.create written using the core library?

        no, it is a primitive (written in C).

        You can't make this kind of allocation explicitly in ocaml. (and you
        don't have to).
        --
        Rémi Vanicat
        vanicat@labri.u-bordeaux.fr
        http://dept-info.labri.u-bordeaux.fr/~vanicat
      • stalkern2
        ... Uhm! Is String.create written using the core library? Ernesto
        Message 3 of 13 , Feb 8, 2002
        View Source
        • 0 Attachment
          Alle ore 06:09, venerdì 08 febbraio 2002, Remi VANICAT ha scritto:
          > String.create 100 will only allocate the 100 byte, it far
          > (very far) more effective

          Uhm! Is String.create written using the core library?

          Ernesto
        Your message has been successfully submitted and would be delivered to recipients shortly.