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

Re: "ocaml_beginners"::[] Printf at runtime

Expand Messages
  • Mark White
    ... Right. So for a trivial application (in this case, I wanted to print integers with a user-specified amount of left zero padding), it s probably easiest to
    Message 1 of 10 , Oct 17, 2006
    • 0 Attachment
      Richard Jones writes:
      > format_of_string ("%"^string_of_int 4^"d") ain't gonna work.

      Right. So for a trivial application (in this case, I wanted
      to print integers with a user-specified amount of left zero
      padding), it's probably easiest to write a function that
      manipulates the string representation itself, and drop the
      result into a literal format with %s.

      William D. Neumann writes:
      > On Fri, 13 Oct 2006, Richard Jones wrote:
      >
      > > You're left with writing your own dynamic printf function.
      >
      > Yeah. You may want to take a look at Olivier Danvy's paper "Functional
      > Unparsing" for som inspiration.
      >
      > http://www.brics.dk/RS/98/12/BRICS-RS-98-12.pdf

      Nice paper. Not sure my OCaml is up to implementing it,
      though! I have a simpler idea based on wrapping up the
      values to be printed in a single type, too, which I might
      try out sometime.

      Thanks,
      Mark <><
    • William Neumann
      ... Well, for something that simple, you should be able to use the * field width specifier to do what you want: # let f x y = Printf.printf %*d n x y;; val f
      Message 2 of 10 , Oct 17, 2006
      • 0 Attachment
        On Oct 17, 2006, at 10:57 AM, Mark White wrote:

        > Right. So for a trivial application (in this case, I wanted
        > to print integers with a user-specified amount of left zero
        > padding), it's probably easiest to write a function that
        > manipulates the string representation itself, and drop the
        > result into a literal format with %s.

        Well, for something that simple, you should be able to use the *
        field width specifier to do what you want:

        # let f x y = Printf.printf "%*d\n" x y;;
        val f : int -> int -> unit = <fun>
        # f 1 3;;
        3
        - : unit = ()
        # f 3 3;;
        3
        - : unit = ()
        # f 10 3;;
        3
        - : unit = ()
        # f 20 3;;
        3
        - : unit = ()

        There appears to be a bug here, however, as you can't seem to mix
        zero padding and optional field width unless you also specify the
        optional leading + sign:

        # let f x y = Printf.printf "%0*d\n" x y;;
        val f : int -> int -> unit = <fun>
        # f 10 3;;
        3
        - : unit = ()
        # let f x y = Printf.printf "%+0*d\n" x y;;
        val f : int -> int -> unit = <fun>
        # f 10 3;;
        +000000003
        # f 20 3;;
        +0000000000000000003
        - : unit = ()

        See the Printf module documentation for more info.

        William D. Neumann

        "I eat T-bone steaks, I lift barbell plates, I'm sweeter than a
        German chocolate cake. I'm the reflection of perfection, the number
        one selection. I'm the man of the hour, the man with the power, too
        sweet to be sour. The ladies' pet, the men's regret, where what you
        see is what you get, and what you don't see, is better yet."

        --Superstar Billy Graham
      • Mark White
        ... Ah, good point; somehow I totally failed to spot that OCaml s printf wrapper understood that. :-D ... In fact this behaves ok for me anyway: Objective Caml
        Message 3 of 10 , Oct 17, 2006
        • 0 Attachment
          William Neumann writes:
          > On Oct 17, 2006, at 10:57 AM, Mark White wrote:
          >
          > > Right. So for a trivial application (in this case, I wanted
          > > to print integers with a user-specified amount of left zero
          > > padding), it's probably easiest to write a function that
          > > manipulates the string representation itself, and drop the
          > > result into a literal format with %s.
          >
          > Well, for something that simple, you should be able to use the *
          > field width specifier to do what you want:

          Ah, good point; somehow I totally failed to spot that
          OCaml's printf wrapper understood that. :-D

          > There appears to be a bug here, however, as you can't seem to mix
          > zero padding and optional field width unless you also specify the
          > optional leading + sign:
          >
          > # let f x y = Printf.printf "%0*d\n" x y;;
          > val f : int -> int -> unit = <fun>
          > # f 10 3;;
          > 3
          > - : unit = ()

          In fact this behaves ok for me anyway:

          Objective Caml version 3.09.2

          # let f x y = Printf.printf "%0*d\n" x y ;;
          val f : int -> int -> unit = <fun>
          # f 10 3 ;;
          0000000003
          - : unit = ()

          Thanks,
          Mark <><
        • William Neumann
          ... Whoops... That was with 3.09.0. They fixed that bug for 3.09.2 (iirc). This works fine now: # let f x y = Printf.printf %0*d n x y;; val f : int - int
          Message 4 of 10 , Oct 17, 2006
          • 0 Attachment
            On Oct 17, 2006, at 12:26 PM, William Neumann wrote:

            > There appears to be a bug here, however, as you can't seem to mix
            > zero padding and optional field width unless you also specify the
            > optional leading + sign:

            Whoops... That was with 3.09.0. They fixed that bug for 3.09.2
            (iirc). This works fine now:

            # let f x y = Printf.printf "%0*d\n" x y;;
            val f : int -> int -> unit = <fun>
            # f 10 3;;
            0000000003
            - : unit = ()
            # f 20 3;;
            00000000000000000003
            - : unit = ()

            William D. Neumann

            "I eat T-bone steaks, I lift barbell plates, I'm sweeter than a
            German chocolate cake. I'm the reflection of perfection, the number
            one selection. I'm the man of the hour, the man with the power, too
            sweet to be sour. The ladies' pet, the men's regret, where what you
            see is what you get, and what you don't see, is better yet."

            --Superstar Billy Graham
          • Mark White
            I m unsure how to (or whether I can) access a non-abstract type defined in another module. In module Gsl_complex, type complex_array = float array is exposed.
            Message 5 of 10 , Jun 25, 2007
            • 0 Attachment
              I'm unsure how to (or whether I can) access a non-abstract
              type defined in another module. In module Gsl_complex,

              type complex_array = float array

              is exposed. In another module, I have a float array I'd
              like to pass where a Gsl_complex.complex_array is needed.
              Can this be done? I get a type mismatch, but I'd thought it
              should be possible if complex_array isn't an abstract type.

              Thanks,
              Mark <><
            • Jon Harrop
              ... I think that should just work. Can you post an explicit example? -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. The OCaml Journal
              Message 6 of 10 , Jun 25, 2007
              • 0 Attachment
                On Monday 25 June 2007 14:41:47 Mark White wrote:
                > I'm unsure how to (or whether I can) access a non-abstract
                > type defined in another module. In module Gsl_complex,
                >
                > type complex_array = float array
                >
                > is exposed. In another module, I have a float array I'd
                > like to pass where a Gsl_complex.complex_array is needed.
                > Can this be done? I get a type mismatch, but I'd thought it
                > should be possible if complex_array isn't an abstract type.

                I think that should just work. Can you post an explicit example?

                --
                Dr Jon D Harrop, Flying Frog Consultancy Ltd.
                The OCaml Journal
                http://www.ffconsultancy.com/products/ocaml_journal/?e
              • Mark White
                ... Ah... think I see the problem; my fault. It turned out to work in the module, but exactly the same line was failing when pasted into my ocamlmktop
                Message 7 of 10 , Jun 25, 2007
                • 0 Attachment
                  Jon Harrop writes:
                  > On Monday 25 June 2007 14:41:47 Mark White wrote:
                  > > I'm unsure how to (or whether I can) access a non-abstract
                  > > type defined in another module. In module Gsl_complex,
                  > >
                  > > type complex_array = float array
                  > >
                  > > is exposed. In another module, I have a float array I'd
                  > > like to pass where a Gsl_complex.complex_array is needed.
                  > > Can this be done? I get a type mismatch, but I'd thought it
                  > > should be possible if complex_array isn't an abstract type.
                  >
                  > I think that should just work. Can you post an explicit example?

                  Ah... think I see the problem; my fault. It turned out to
                  work in the module, but exactly the same line was failing
                  when pasted into my ocamlmktop toplevel.

                  It seems the toplevel couldn't find the GSL cmi files, so
                  couldn't do that type interference for interactive code.
                  Adding a -I at runtime fixed it. :-)

                  Thanks,
                  Mark <><
                Your message has been successfully submitted and would be delivered to recipients shortly.