caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] printf hook
@ 2002-06-26 15:01 Nicolas Cannasse
  2002-06-26 15:21 ` Jean-Christophe Filliatre
  2002-06-26 15:51 ` Remi VANICAT
  0 siblings, 2 replies; 7+ messages in thread
From: Nicolas Cannasse @ 2002-06-26 15:01 UTC (permalink / raw)
  To: OCaml

Hi list !
I would like do to something like this :

let sprint msg =
    print_endline ("BEGIN: "^(sprintf msg))

without of course loosing the ability of having variable number of
parameters in my "msg".
Is that kind of thing possible ?

Thanks a Lot
Nicolas Cannasse

-------------------
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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] printf hook
  2002-06-26 15:01 [Caml-list] printf hook Nicolas Cannasse
@ 2002-06-26 15:21 ` Jean-Christophe Filliatre
  2002-07-12 10:21   ` Nicolas Cannasse
  2002-06-26 15:51 ` Remi VANICAT
  1 sibling, 1 reply; 7+ messages in thread
From: Jean-Christophe Filliatre @ 2002-06-26 15:21 UTC (permalink / raw)
  To: Nicolas Cannasse; +Cc: OCaml


Nicolas Cannasse writes:
 > Hi list !
 > I would like do to something like this :
 > 
 > let sprint msg =
 >     print_endline ("BEGIN: "^(sprintf msg))
 > 
 > without of course loosing the ability of having variable number of
 > parameters in my "msg".
 > Is that kind of thing possible ?

You can do the following :

======================================================================
# open Printf;;
# let print x = printf "BEGIN: "; printf x;;               
======================================================================

but this function still expects a format as first argument :

======================================================================
# print "foo\n";;
BEGIN: foo
- : unit = ()
# print "foo = %d\n" 2;;
BEGIN: foo = 2
- : unit = ()
# print "foo = %d * %d\n" 2 3;;
BEGIN: foo = 2 * 3
- : unit = ()
======================================================================

-- 
Jean-Christophe 

-------------------
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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] printf hook
  2002-06-26 15:01 [Caml-list] printf hook Nicolas Cannasse
  2002-06-26 15:21 ` Jean-Christophe Filliatre
@ 2002-06-26 15:51 ` Remi VANICAT
  1 sibling, 0 replies; 7+ messages in thread
From: Remi VANICAT @ 2002-06-26 15:51 UTC (permalink / raw)
  To: caml-list

"Nicolas Cannasse" <warplayer@free.fr> writes:

> Hi list !
> I would like do to something like this :
>
> let sprint msg =
>     print_endline ("BEGIN: "^(sprintf msg))
>
> without of course loosing the ability of having variable number of
> parameters in my "msg".
> Is that kind of thing possible ?

For just this, you could do :

let sprint msg =
   print_string "BEGIN: ";
   printf msg;;

But generally speaking, you need kprintf from the 
Not yet, it need kprintf that is in the development version of ocaml:

# Printf.kprintf;;
- : (string -> string) -> ('a, unit, string) format -> 'a = <fun>

so it would be something as :

let sprint msg = 
  let aux res = 
    print_endline ("BEGIN: " ^ res);
    res in
  kprintf res msg;;

-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] printf hook
  2002-06-26 15:21 ` Jean-Christophe Filliatre
@ 2002-07-12 10:21   ` Nicolas Cannasse
  2002-07-12 12:41     ` Pierre Weis
  0 siblings, 1 reply; 7+ messages in thread
From: Nicolas Cannasse @ 2002-07-12 10:21 UTC (permalink / raw)
  To: Jean-Christophe Filliatre; +Cc: OCaml

>  > I would like do to something like this :
>  > 
>  > let sprint msg =
>  >     print_endline ("BEGIN: "^(sprintf msg))
>  > 
>  > without of course loosing the ability of having variable number of
>  > parameters in my "msg".
>  > Is that kind of thing possible ?
> 
> You can do the following :
> 
> # open Printf;;
> # let print x = printf "BEGIN: "; printf x;;               

I just found something quite funny :

let print x =
    printf "BEGIN";
    printf x;
    flush stdout

print "test";;
print "%d" 10; (** TOO MUCH ARGS **)

============

let print x =
    printf "BEGIN";
    let r = printf x in
    flush stdout;
    r

print "test";
print "%d" 10; (** WORKING **)

NC

-------------------
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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] printf hook
  2002-07-12 10:21   ` Nicolas Cannasse
@ 2002-07-12 12:41     ` Pierre Weis
  0 siblings, 0 replies; 7+ messages in thread
From: Pierre Weis @ 2002-07-12 12:41 UTC (permalink / raw)
  To: Nicolas Cannasse; +Cc: Jean-Christophe.Filliatre, caml-list

> I just found something quite funny :
> 
> let print x =
>     printf "BEGIN";
>     printf x;
>     flush stdout
> 
> print "test";;
> print "%d" 10; (** TOO MUCH ARGS **)

This is normal, isn't it ?

> ============
> 
> let print x =
>     printf "BEGIN";
>     let r = printf x in
>     flush stdout;
>     r
> 
> print "test";
> print "%d" 10; (** WORKING **)

This is also perfectly regular: your code

let print x =
  printf "BEGIN";
  let r = printf x in
  flush stdout;
  r

is functionally equivalent to

let print x =
  let r = printf x in
  r

which is again functionally equivalent to

let print x = printf x

which is again equivalent to

let print = printf

The type checker gives an hint on that fact, since print is reported
having the same type as printf:

        Objective Caml version 3.04+15 (2002-06-18)

#open Printf;;
# let print x =
  printf "BEGIN";
  let r = printf x in
  flush stdout;
  r;;
val print : ('a, out_channel, unit) format -> 'a = <fun>
# printf;;
- : ('a, out_channel, unit) format -> 'a = <fun>

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, 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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] printf hook
  2002-06-26 16:31 Damien Doligez
@ 2002-06-26 16:46 ` Nicolas Cannasse
  0 siblings, 0 replies; 7+ messages in thread
From: Nicolas Cannasse @ 2002-06-26 16:46 UTC (permalink / raw)
  To: Damien Doligez, caml-list

> >let sprint msg =
> >    print_endline ("BEGIN: "^(sprintf msg))
> >
> >without of course loosing the ability of having variable number of
> >parameters in my "msg".
> 
> If you can wait for 3.05, or if you are willing to use the CVS
> version, you'll be able to write:
> 
>   let sprint fmt =
>     let do_output s = print_endline ("BEGIN: " ^ s); "" in
>     Printf.kprintf do_output fmt
>   ;;
> 
> The tail-call to kprintf allows it to get the extra arguments thanks
> to currying.  The do_output function must return a string (which is
> returned by sprint) for technical reasons.  (This may change before
> the release.)

Seems nice :)
I'll wait the official release of 3.05 before switching to kprintf.
Thanks to all people who answered.

Nicolas Cannasse

-------------------
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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re:  [Caml-list] printf hook
@ 2002-06-26 16:31 Damien Doligez
  2002-06-26 16:46 ` Nicolas Cannasse
  0 siblings, 1 reply; 7+ messages in thread
From: Damien Doligez @ 2002-06-26 16:31 UTC (permalink / raw)
  To: caml-list, warplayer

>From: "Nicolas Cannasse" <warplayer@free.fr>
>
>let sprint msg =
>    print_endline ("BEGIN: "^(sprintf msg))
>
>without of course loosing the ability of having variable number of
>parameters in my "msg".

If you can wait for 3.05, or if you are willing to use the CVS
version, you'll be able to write:

  let sprint fmt =
    let do_output s = print_endline ("BEGIN: " ^ s); "" in
    Printf.kprintf do_output fmt
  ;;

The tail-call to kprintf allows it to get the extra arguments thanks
to currying.  The do_output function must return a string (which is
returned by sprint) for technical reasons.  (This may change before
the release.)

-- Damien
-------------------
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


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2002-07-12 12:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-06-26 15:01 [Caml-list] printf hook Nicolas Cannasse
2002-06-26 15:21 ` Jean-Christophe Filliatre
2002-07-12 10:21   ` Nicolas Cannasse
2002-07-12 12:41     ` Pierre Weis
2002-06-26 15:51 ` Remi VANICAT
2002-06-26 16:31 Damien Doligez
2002-06-26 16:46 ` Nicolas Cannasse

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).