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

Partial applications

Expand Messages
  • Mark White
    How (if at all) could I go about writing a wrapper function to sprintf which does something with the string sprintf returns -- say, prints it with parentheses
    Message 1 of 3 , Jul 7, 2006
      How (if at all) could I go about writing a wrapper function
      to sprintf which does something with the string sprintf
      returns -- say, prints it with parentheses at each end?

      # pfwrap "%d" 5 ;;
      (5)

      Something which modifies the _arguments_ to printf and
      returns a partially applied function is straightforward:

      let pfwrap fmt =
      Printf.printf ( "(" ^^ fmt ^^ ")\n" )

      but that's not really what I mean, because I'm interested in
      a general operation on result, not just changing the args.
      Some way of saying:

      print_in_parens (sprintf "format" arg2 arg2 ...)

      without writing out the sprintf call. (I suspect I've not
      really understood the relationship between strings and
      formats here, either...)

      Mark <><
    • Jonathan Roewen
      ... # let pfwrap fmt = let fmt = ( ^^ fmt ^^ ) in sprintf fmt;; val pfwrap : ( a, unit, string, string) format4 - a = # pfwrap %d,%d 34 45;; - :
      Message 2 of 3 , Jul 7, 2006
        > How (if at all) could I go about writing a wrapper function
        > to sprintf which does something with the string sprintf
        > returns -- say, prints it with parentheses at each end?
        >
        > # pfwrap "%d" 5 ;;
        > (5)

        # let pfwrap fmt =
        let fmt = "(" ^^ fmt ^^ ")" in
        sprintf fmt;;
        val pfwrap : ('a, unit, string, string) format4 -> 'a = <fun>
        # pfwrap "%d,%d" 34 45;;
        - : string = "(34,45)"

        (^^) concatenates format strings (defined in Pervasives)

        Jonathan
      • Thierry Martinez
        ... ksprintf print_in_parens ... I am afraid there is no relationship...
        Message 3 of 3 , Jul 7, 2006
          Mark White:
          > Some way of saying:
          >
          > print_in_parens (sprintf "format" arg2 arg2 ...)

          ksprintf print_in_parens

          > (I suspect I've not
          > really understood the relationship between strings and
          > formats here, either...)

          I am afraid there is no relationship...
        Your message has been successfully submitted and would be delivered to recipients shortly.