vault backup: 2023-09-12 15:45:33
This commit is contained in:
parent
c380a6ff4d
commit
692b0addf2
@ -38,6 +38,7 @@ toto
|
|||||||
```
|
```
|
||||||
|
|
||||||
## 0.2. Sequence of prints
|
## 0.2. Sequence of prints
|
||||||
|
To print two things on the same line we have to use this structure
|
||||||
```Ocaml
|
```Ocaml
|
||||||
# print_string "The answer is: "; print_int 42;;
|
# print_string "The answer is: "; print_int 42;;
|
||||||
The answer is: 42 -: unit = ()
|
The answer is: 42 -: unit = ()
|
||||||
@ -48,3 +49,29 @@ Warning 10 [non-unit-statement]: this expression should have type unit.
|
|||||||
|
|
||||||
# print_string "The answer is: "; 3*2*7;;
|
# print_string "The answer is: "; 3*2*7;;
|
||||||
The answer is: -:int = 42
|
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 = <fun>
|
||||||
|
|
||||||
|
# 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()
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user