Version française
Home     About     Download     Resources     Contact us    
Browse thread
Pervasives or Printf module ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Anil Madhavapeddy <anil@r...>
Subject: Re: [Caml-list] Pervasives or Printf module ?
On Sun, Jan 22, 2006 at 03:34:04PM +0000, Jon Harrop wrote:
> 
> You can do some nice things with printf, like exploit currying:
> 
> # List.iter (Printf.printf "%d ") [1;2;3];;
> 1 2 3 - : unit = ()
> 
> but it is easy to trip up:
> 
> # List.iter (Printf.printf " %d") [1;2;3];;
>  123- : unit = ()
> 
> Note: the former prints a space after every integer whereas the latter prints 
> only a single space at the beginning. To get the latter to act as the former 
> does, you must beta expand (I think that is the correct technical term) to 
> postpone the computation of printf " ":

This has been improved in 3.09.1 ... from the Changelog:

- Printf: better handling of partial applications of the
  printf functions.

        Objective Caml version 3.09.1

# List.iter (Printf.printf "%d ") [1;2;3] ;;
1 2 3 - : unit = ()
# List.iter (Printf.printf " %d") [1;2;3] ;;
 1 2 3- : unit = ()
# List.iter (fun n -> Printf.printf " %d" n) [1;2;3] ;;
 1 2 3- : unit = ()

Hurrah!  It always tripped me up :)

-- 
Anil Madhavapeddy                                 http://anil.recoil.org
University of Cambridge                          http://www.cl.cam.ac.uk