« Premiers pas en OCaml/Fonctions utilitaires » : différence entre les versions

Contenu supprimé Contenu ajouté
Watermy (discussion | contributions)
m + catégorie + num chapitre
Watermy (discussion | contributions)
Explication supplémentaire
Ligne 16 :
<source lang="ocaml">
# <type de sortie>_of_<type d'entrée> <valeur d'entrée>;;
 
- : <type de sortie> = <valeur de sortie>
</source>
}}
 
Le mot anglais '''of''' peut être traduit par '''de''' en français.
 
=== Réel vers entier ===
Ligne 35 ⟶ 38 :
<source lang="ocaml">
# float_of_int;;
(* Sucre syntaxique *)
# (float);;
- : int -> float = <fun>
 
# float_of_int 13;;
# (float) 13;;
- : float = 13.
</source>
Ligne 65 ⟶ 71 :
</source>
 
=== Booléen vers chaîne de caractèrecaractères ===
<source lang="ocaml">
# string_of_bool;;
Ligne 72 ⟶ 78 :
# string_of_bool true;;
- : string = "true"
 
# string_of_bool false;;
- : string = "false"
</source>
 
=== Chaîne de caractèrecaractères vers booléen ===
Attention la fonction de conversion d'une chaîne de caractère vers un booléen est sensible à la casse. <br />
Les deux seules valeurs accepté sont en minuscules '''"true"''' et '''"false"'''.
Ligne 85 ⟶ 92 :
# bool_of_string "true";;
- : bool = true
 
# bool_of_string "false";;
- : bool = false
Ligne 91 ⟶ 99 :
</source>
 
=== Chaîne de caractèrecaractères vers nombre ===
Le chaîne doit correspondre un en entier ou un réel. Attention de bien utilisé un point et pas une virgule.
<source lang="ocaml">
Ligne 102 ⟶ 110 :
# float_of_string "13.37";;
- : float = 13.37
 
# float_of_string "13,37";;
Exception: Failure "float_of_string".
</source>
 
=== Nombre vers chaîne de caractèrecaractères ===
 
<source lang="ocaml">
Ligne 121 ⟶ 130 :
== Les fonctions d'entrées ==
 
Les fonctions d'entrées commencent par le mot anglais '''read''' qui signifie '''lire''' (verbe) ou '''lecture''' (nom) en français.
 
=== Lecture d'une chaîne de caractères ===
<source lang="ocaml">
# read_line;;
- : unit -> string = <fun>
 
# read_line ();;
read_int;;
</source>
 
=== Lecture d'un entier ===
<source lang="ocaml">
# read_int;;
- : unit -> int = <fun>
 
# read_int ();;
read_float;;
</source>
 
=== Lecture d'un réel ===
<source lang="ocaml">
# read_float;;
- : unit -> float = <fun>
 
# read_float ();;
</source>
 
== Les fonctions d'affichages ==
 
Les fonctions d'affichage commencent par le mot anglais '''print''' qui signifie '''afficher''' (verbe) ou '''affichage''' (nom) en français.
 
=== Affichage d'un caractère ===
<source lang="ocaml">
# print_char;;
- : char -> unit = <fun>
 
# print_char 'A';;
</source>
 
=== Affichage d'un entier ===
 
<source lang="ocaml">
# print_int;;
- : int -> unit = <fun>
 
# print_int 1337;;
</source>
 
=== Affichage d'un réel ===
 
<source lang="ocaml">
# print_float;;
- : float -> unit = <fun>
 
# print_float 13.37;;
</source>
 
=== Affiche d'une chaine de caractères ===
 
Sans saut de ligne.
<source lang="ocaml">
# print_string;;
- : string -> unit = <fun>
 
# print_string "Bonjour le monde !";;
</source>
 
Avec saut de ligne.
<source lang="ocaml">
# print_endline;;
- : string -> unit = <fun>
 
# print_endline "Bonjour le monde !";;
</source>
 
=== Passer une ligne ===
Pour sauter une ligne.
<source lang="ocaml">
# print_newline;;
- : unit -> unit = <fun>
 
# print_newline();;
</source>
 
== Concaténation de chaîne de caractères ==
Vous pouvez concaténer deux chaînes de caractères grâce à l'opérateur accent circonflexe '''^'''.
<source lang="ocaml">
#(^);;
 
# "Bonjour " ^ "le " ^ "monde " ^ "!";;
</source>
 
== Récupérer les arguments du programme ==
Si vous voulez récupérer des arguments que vous avez passer à votre programme.
<source lang="ocaml">
# Sys.argv.(1);;
 
</source>
 
== Référence ==
{{en}} lienWeb|format=html|licence=copyright|langue=fr|url=http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html }}
 
[[Catégorie:Premiers pas en OCaml]]