* Pervasives or Printf module ?
@ 2006-01-22 15:10 sejourne_kevin
2006-01-22 15:34 ` [Caml-list] " Jon Harrop
0 siblings, 1 reply; 6+ messages in thread
From: sejourne_kevin @ 2006-01-22 15:10 UTC (permalink / raw)
To: caml-list
Hi list,
As the usage of:
Printf.sprintf "_%d" x
and
"_"^(string_of_int x)
is the same,
what is the
better solution?
Do you have an opinion for the one or the second?
Kévin.
___________________________________________________________________________
Nouveau : téléphonez moins cher avec Yahoo! Messenger ! Découvez les tarifs exceptionnels pour appeler la France et l'international.
Téléchargez sur http://fr.messenger.yahoo.com
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Pervasives or Printf module ?
2006-01-22 15:10 Pervasives or Printf module ? sejourne_kevin
@ 2006-01-22 15:34 ` Jon Harrop
2006-01-22 16:27 ` Anil Madhavapeddy
2006-01-22 16:46 ` Vincenzo Ciancia
0 siblings, 2 replies; 6+ messages in thread
From: Jon Harrop @ 2006-01-22 15:34 UTC (permalink / raw)
Cc: caml-list
On Sunday 22 January 2006 15:10, sejourne_kevin wrote:
> As the usage of:
> Printf.sprintf "_%d" x
> and
> "_"^(string_of_int x)
That can be written without parentheses and the style guide advises the use of
spaces around operators (i.e. "^"):
"_" ^ string_of_int x
> is the same, what is the better solution?
>
> Do you have an opinion for the one or the second?
I tend to avoid Printf and stick with Pervasives where possible.
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 " ":
# List.iter (fun n -> Printf.printf " %d" n) [1;2;3];;
1 2 3- : unit = ()
If efficiency is a concern then I would expect printf to be faster because it
should avoid string concatenation. I haven't tested that though...
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Pervasives or Printf module ?
2006-01-22 15:34 ` [Caml-list] " Jon Harrop
@ 2006-01-22 16:27 ` Anil Madhavapeddy
2006-01-22 16:42 ` sejourne_kevin
2006-01-22 16:46 ` Vincenzo Ciancia
1 sibling, 1 reply; 6+ messages in thread
From: Anil Madhavapeddy @ 2006-01-22 16:27 UTC (permalink / raw)
To: Jon Harrop; +Cc: caml-list
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Pervasives or Printf module ?
2006-01-22 16:27 ` Anil Madhavapeddy
@ 2006-01-22 16:42 ` sejourne_kevin
0 siblings, 0 replies; 6+ messages in thread
From: sejourne_kevin @ 2006-01-22 16:42 UTC (permalink / raw)
To: caml-list
Anil Madhavapeddy a écrit :
> 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 :)
Hurrah!
___________________________________________________________________________
Nouveau : téléphonez moins cher avec Yahoo! Messenger ! Découvez les tarifs exceptionnels pour appeler la France et l'international.
Téléchargez sur http://fr.messenger.yahoo.com
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Pervasives or Printf module ?
2006-01-22 15:34 ` [Caml-list] " Jon Harrop
2006-01-22 16:27 ` Anil Madhavapeddy
@ 2006-01-22 16:46 ` Vincenzo Ciancia
2006-01-23 9:18 ` [Caml-list] " Remi Vanicat
1 sibling, 1 reply; 6+ messages in thread
From: Vincenzo Ciancia @ 2006-01-22 16:46 UTC (permalink / raw)
To: caml-list
Jon Harrop wrote:
> To get the latter to act as the former
> does, you must beta expand (I think that is the correct technical term)
In computer science literature, alpha conversion is usually the renaming of
a bound variable (fun x -> T = fun y -> T[y/x] provided that y does not
occurr in T), beta _reduction_ is function application ((fun x -> T)(T') =
T[T'/x]) , and _eta_ expansion is the term rewriting you're looking for.
I don't know why, maybe it's just the order in which they were enumerated
originarily, but where are gamma, delta, epsilon, zeta rewrite rules? :)
V.
--
Please note that I do not read the e-mail address used in the from field but
I read vincenzo_ml at yahoo dot it
Attenzione: non leggo l'indirizzo di posta usato nel campo from, ma leggo
vincenzo_ml at yahoo dot it
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Re: Pervasives or Printf module ?
2006-01-22 16:46 ` Vincenzo Ciancia
@ 2006-01-23 9:18 ` Remi Vanicat
0 siblings, 0 replies; 6+ messages in thread
From: Remi Vanicat @ 2006-01-23 9:18 UTC (permalink / raw)
To: caml-list
2006/1/22, Vincenzo Ciancia <vincenzo_yahoo_addressguard-gmane@yahoo.it>:
> Jon Harrop wrote:
>
> > To get the latter to act as the former
> > does, you must beta expand (I think that is the correct technical term)
>
> In computer science literature, alpha conversion is usually the renaming of
> a bound variable (fun x -> T = fun y -> T[y/x] provided that y does not
> occurr in T), beta _reduction_ is function application ((fun x -> T)(T') =
> T[T'/x]) , and _eta_ expansion is the term rewriting you're looking for.
>
> I don't know why, maybe it's just the order in which they were enumerated
> originarily, but where are gamma, delta, epsilon, zeta rewrite rules? :)
In the coq manual, one can see severall other rewriting rule, like
iota-reduction, delta-reduction and zeta-reduction.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2006-01-23 9:18 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-01-22 15:10 Pervasives or Printf module ? sejourne_kevin
2006-01-22 15:34 ` [Caml-list] " Jon Harrop
2006-01-22 16:27 ` Anil Madhavapeddy
2006-01-22 16:42 ` sejourne_kevin
2006-01-22 16:46 ` Vincenzo Ciancia
2006-01-23 9:18 ` [Caml-list] " Remi Vanicat
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox