Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Passing printf format strings to functions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: weis@y...
Subject: Re: [Caml-list] Passing printf format strings to functions
> Hi,
> 
> Am Tuesday 14 September 2004 23:55 schrieb Erik de Castro Lopo:
> > Hi all,
> >     let rec print_string_pairs (fmt:string) =
> >         function
> >             [] -> print_endline ""
> >
> >         |    a :: b ->
> >
> >                 Printf.printf fmt (fst a) (snd a) ;
> >                 print_string_pairs fmt b
> >         ;;
> 
> 
> I think the fmt variable has not to be of type string but of some
> format type.

Exactly. Hence you just have to remove the wrong type constraint
(fmt:string) and simply write fmt, to get it working beautifully:

val print_string_pairs :
  ('a -> 'b -> 'c, out_channel, unit) format -> ('a * 'b) list -> unit

>  there exists a conversion function in Pervasives:
> val format_of_string : ('a, 'b, 'c, 'd) format4 -> ('a, 'b, 'c, 'd) format4
>   (*format_of_string s returns a format string read from the string literal 
> s.*)

In this case I doubt you ever need to use this function: the type-checker
will do the job automatically for you!

For instance:

# print_string_pairs "(%s, %s)";;  
- : (string * string) list -> unit = <fun>

Or even:

# print_string_pairs "(%i, %i)";;
- : (int * int) list -> unit = <fun>

At this point, you should consider renaming your function, given its
highly polymorphic nature, it shoould be promoted to print_pairs ...

Best regards,

-- 
Pierre Weis

INRIA, Projet Cristal, http://pauillac.inria.fr/~weis

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners