"Question 1";;
2;;
"il faut mettre ;; à la fin de chaque ligne";;
2.;;
"hello";;
'a';;
let a = 3;;
a + a;;
a=3;;
let f a = 2*a;; 
"on définit la fonction f,";;
f 5;; 
"le programme n'affiche rien en sortie , * et + fonctionnent entre 2 entiers ";;
"f 1.5";; 
"mais pas avec des nombres dédimaux";;
"2 + 3.5 : idem";; 
2. +. 3.5;;
"il faut donc utiliser +. et *.";;
7 mod 3;;
"le reste de la division euclidienne de 7 par 3";;
let g a = a +. 2.;;
"g est définie pour des entiers";;
let u = 2 and v = 3 in u + v;;
"u + v";;
"on ne peut additionner directement 2 variables";;
"exp 2";;
"exp est définie mais pour des float, comme les fonctions usuelles";;
log (exp 1.);;
2.** 3.;;
let racine x = int_of_float (sqrt (float_of_int x));;
"on peut transformer des entiers en décimaux et inversement avec int_of_float";;
racine 24;;
"on peut donc mettre ici en entrée des entiers";;
let ajoute a b = a + b;;
ajoute 2 3;;
let incremente1 a = ajoute 1 a;;
"on peut utilier des fonctions dans d'autres fonctions";;
incremente1 3;;
let incremente2 = ajoute 1;;
incremente2 3;;
(3,4);;
"couple";;
let echange (x,y)=(y,x);; 
3<5;;
3.5>5.5;;
"2<3.5";;
"les opérateurs < et > existent mais pour des nombres du même type à nouveau";;
let phi x = match x with
| v when v < 0. -> -. v *. v
| v -> v *. v;;
"pour avoir des fonctions sur plusieurs lignes, on met |, match argument with, sans ;; à la fin.";;
"dans la fonction, les variables ne s'appellent plus comme celle en argument, on met des -> pour leur attribuer une valeur";;
let compte_points score1 score2 = match score1,score2 with
| (a,b) when a = b -> 0
| (a,b) when a > b -> 1
| _ -> -1;;
"en termes d'instructions conditionnelles, on a when autant qu'on veut puis ensuite rien pour le 'sinon'";;

"Question 2";;
let distance (a,b) (c,d) = int_of_float (sqrt ((float_of_int(a + b))** 2. +. (float_of_int(c + d))**2.));;
let norme (a,b) (c,d) = float_of_int ( distance (a,b) (c,d))** 2.;;

"Question 3";;
let applique f x = f x;;

"Question 4";;
let composee g f x = g applique f x;;

"Question 5";;
let bisextile a = match a with 
| b when b mod 400 = 0 -> 1
| b when b mod 100 = 0 -> 0
| b when b mod 4 =0 -> 1
| b -> 0;;

let nombre_de_jours mois annee = match mois,annee with
| (a,b) when a > 7 -> 31 - (a mod 2)
| (a,b) when a = 2 -> 28 + bisextile(b)
| (a,b) -> 30 + (a mod 2);;
 

OCaml Online Compiler

Write, Run & Share OCaml code online using OneCompiler's OCaml online compiler for free. It's one of the robust, feature-rich online compilers for OCaml language, running on the latest version 4. Getting started with the OneCompiler's OCaml compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as OCaml. OneCompiler also has reference programs, where you can look for the sampleprograms and start coding.

About OCaml

OCaml is general purpose programming language with more importance to safety and expressiveness. With it's advanced type system, it helps to catch the mistakes in an efficient way. Hence this is used to develop applications/environments where a single mistake can cost millions and speed matters. It has good community support and rich set of development tools and libraries.

key features

  • Strongly typed functional language
  • Easy to learn
  • Very powerful type system
  • Automatic memory management
  • There is a seperate compilation of standalone applications.
  • Ocaml compiler can also produce machine codes
  • Multiple inheritance and parametric classes etc can be expressed in a simpler way
  • User can define algebraic data types

Data types

ClassificationData types
Basic data typesintegers, floating point numbers, booleans, characters, strings
Sophisticated data typestuples, arrays, lists, sets, hash tables, queues, stacks, data streams

Note:

OCaml allows users to define new data types.

Variables

Variable is a name given to the storage area in order to manipulate them in our programs.

let varible-names = value

Loops

1. If:

If is performed when you need to choose expression based on a boolean-condition.

if boolean-condition then (* code if condition is true *)
  
if boolean-condition then (* code if condition is true*) else (* code if condition is false*)

2. While:

While is used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while boolean-condition do
  (* code *)
done

3. For:

For loop is used to iterate a set of statements for specific number of items.

for var = start-value to end-value do
  (* code *)
done
  
for var = start-value downto end-value do
  (* code *)
done