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
        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
        Message 3 of 13 , Feb 8, 2002
        View Source
        • 0 Attachment
          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

          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)

          I think that it is very interesting to write copy() as recursive: so the "max
          size" of the file can be also very low and all of the file will be
          recursively read anyway. Great!
          Thank you
          Ernesto

          Alle ore 14:41, giovedì 07 febbraio 2002, doug+ml.ocaml_beginners@...
          ha scritto:
          >  let copy_file file_in file_out =
          >   let ic = open_in file_in in
          >   let max = 4096 in
          >   let buf = String.create max in
          >   try
          >     let oc = open_out file_out in
          >     (try
          >        let rec copy () =
          >          let len = input ic buf 0 max in
          >          if len > 0 then (output oc buf 0 len; copy ()) in
          >        copy ();
          >        close_in ic;
          >        close_out oc;
          >      with e -> close_out oc; raise e)
          >   with e -> close_in ic; raise e;;
        • stalkern2
          ... Uhm! Is String.create written using the core library? Ernesto
          Message 4 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.