Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug avec les string et Printf #2641

Closed
vicuna opened this issue Dec 14, 2000 · 1 comment
Closed

Bug avec les string et Printf #2641

vicuna opened this issue Dec 14, 2000 · 1 comment
Labels

Comments

@vicuna
Copy link

vicuna commented Dec 14, 2000

Original bug ID: 247
Reporter: administrator
Status: closed
Resolution: not a bug
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)

Bug description

Full_Name: Benjamin Monate
Version: 3.0
OS: Linux
Submission from: pc8-118.lri.fr (129.175.8.118)

Bonjour,
Il semble y avoir un problemes avec les chaines constantes utilisees plusieurs
fois:

ocaml
Objective Caml version 3.00

List.iter (Printf.printf "a%sb") ["1";"2";"3"];;

a1b2b3b- : unit = ()

On attendrait evidemment: a1ba2ba3b

Merci

@vicuna
Copy link
Author

vicuna commented Dec 14, 2000

Comment author: administrator

Full_Name: Benjamin Monate
Version: 3.0
OS: Linux
Submission from: pc8-118.lri.fr (129.175.8.118)

Bonjour,
Il semble y avoir un problemes avec les chaines constantes utilisees plusieurs
fois:

ocaml
Objective Caml version 3.00

List.iter (Printf.printf "a%sb") ["1";"2";"3"];;

a1b2b3b- : unit = ()

On attendrait evidemment: a1ba2ba3b

Merci

Ce n'est pas un bug, mais une particularité de la fonction printf,
combinée à la sémantique habituelle du langage.

Printf.printf "a%sb" est une abréviation pour

print_string "a"; fun s -> print_string s; print_string "b"

Ce que vous observez en évaluant:

Printf.printf "a%sb";;

a- : string -> unit =

Cela implique que vous ne faîtes qu'itérer la fonction
fun s -> print_string s; print_string "b" sur la liste de nombre.

Ainsi
List.iter (Printf.printf "a%sb") ["1";"2";"3"];;
est équivalent à:

print_string "a";
List.iter (fun s -> print_string s; print_string "b") ["1";"2";"3"];;

qui imprime a1b2b3b, comme on s'y attend. D'ailleurs une décomposition
en deux phrases le rend évident:

let f = Printf.printf "a%sb";;

aval f : string -> unit =

List.iter f ["1";"2";"3"];;

1b2b3b- : unit = ()

Cordialement,

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/

@vicuna vicuna closed this as completed Jan 8, 2001
@vicuna vicuna added the bug label Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant