diff --git a/Algo/Séminaire/Chapter 4 - A bit of imperative.md b/Algo/Séminaire/Chapter 4 - A bit of imperative.md index 3d3b155..eb74e2b 100644 --- a/Algo/Séminaire/Chapter 4 - A bit of imperative.md +++ b/Algo/Séminaire/Chapter 4 - A bit of imperative.md @@ -38,6 +38,7 @@ toto ``` ## 0.2. Sequence of prints +To print two things on the same line we have to use this structure ```Ocaml # print_string "The answer is: "; print_int 42;; The answer is: 42 -: unit = () @@ -47,4 +48,30 @@ The answer is -: unit = () Warning 10 [non-unit-statement]: this expression should have type unit. # print_string "The answer is: "; 3*2*7;; -The answer is: -:int = 42 \ No newline at end of file +The answer is: -:int = 42 +``` + +### 0.3. Print in a function +```OCaml +# let print_even n = + if n mod 2 = 0 then + print_int n;; + (*Caml will automaticaly add the else with a empty value ()*) +val print_even: int -> unit = + +# print_even 4;; +4 -: unit = () + +# print_even 7;; +-: unit = () + +# let ret_even n = + if n mod 2 = 0 then + n;; +Error: should have int type (*because of the hidden else*) + +# let print_even n = + if n mod 2 = 0 then + print_int n ; + print_newline() + \ No newline at end of file