GnuVince
05-28-2002, 08:47 PM
Small introduction to O'Caml
O'Caml is a computer programming language. Its homepage is at:
http://caml.inria.fr
O'Caml is truely a wonderful language once you know it, but the first
steps are VERY hard, and hopefully this tutorial will make things
easier. First off, O'Caml is not a procedural language: it belongs in
the functional languages family. Pure functional languages provide no
loop mecanism (you loop using recursion), once set, variables cannot be
altered, etc. Fortuneatly, O'Caml is not a pure functionnal language,
and loops and mutable variables are available. So let's start!
First, start the O'Caml top-level loop my running `ocaml' at your
command prompt, you should see something like this:
Objective Caml version 3.04
#
The # is the O'Caml prompt, so you are ready to start.
1. Types
O'caml is language that is strictly typed: this means that once set, a
variable cannot change types, and you cannot do operations between two
type (e.g: adding an integer and a float). With this in mind, let's try
to do some basic maths:
# 2 + 2;;
- : int = 4
the ';;' is used to say when an expression is finished, it's like a
period in a phrase. The second line shows the result (4) and the type
of the result (int). I was talking that types cannot be mixed, let's
see what happens when we try
# 2 + 2.5;;
This expression has type float but is here used with type int
2.5 is underlined and you have the error message on the second line.
What does it tell you? Well, 2.5 which is of type float is used with a
function that only manipulates integers (+). So how do you add them?
The addition operator for floats it '+.' (a plus with a dot after to
denote that it works with floats). So if we try our operation again,
but with +., we get:
# 2 +. 2.5;;
This expression has type int but is here used with type float
Now the 2 is underlined and ocaml complains that an int is being used
with a float operator. So how are we going to make this sum? Well, we
need to convert one of the numbers to the type of the other. If we
convert 2.5 to int, we lose the .5 (which is important), so we'll
convert 2 to be a float.
# (float_of_int 2) +. 2.5;;
- : float = 4.5
What is float_of_int? Let's ask ocaml!
# float_of_int;;
- : int -> float = <fun>
O'caml tells us that float_of_int is a function (<fun>) that takes an
integer argument (int) and returns a float value. So when you gave
float_of_int the argument 2, it returned the same thing, but with type
float so that the operation could be made.
So remember to always work the same type!
List of Arithmetic operators for integers:
+ : Addition
- : Substraction
* : Multiplication
/ : Division
mod : Modulo
List of Arithmetic operators for floats:
+. : Addition
-. : Substraction
*. : Multiplication
/. : Division
** : Exponentiation
To convert an integer to a float: float_of_int n
To convert a float to an integer: int_of_float n
That will be all for now. Next time: variables
O'Caml is a computer programming language. Its homepage is at:
http://caml.inria.fr
O'Caml is truely a wonderful language once you know it, but the first
steps are VERY hard, and hopefully this tutorial will make things
easier. First off, O'Caml is not a procedural language: it belongs in
the functional languages family. Pure functional languages provide no
loop mecanism (you loop using recursion), once set, variables cannot be
altered, etc. Fortuneatly, O'Caml is not a pure functionnal language,
and loops and mutable variables are available. So let's start!
First, start the O'Caml top-level loop my running `ocaml' at your
command prompt, you should see something like this:
Objective Caml version 3.04
#
The # is the O'Caml prompt, so you are ready to start.
1. Types
O'caml is language that is strictly typed: this means that once set, a
variable cannot change types, and you cannot do operations between two
type (e.g: adding an integer and a float). With this in mind, let's try
to do some basic maths:
# 2 + 2;;
- : int = 4
the ';;' is used to say when an expression is finished, it's like a
period in a phrase. The second line shows the result (4) and the type
of the result (int). I was talking that types cannot be mixed, let's
see what happens when we try
# 2 + 2.5;;
This expression has type float but is here used with type int
2.5 is underlined and you have the error message on the second line.
What does it tell you? Well, 2.5 which is of type float is used with a
function that only manipulates integers (+). So how do you add them?
The addition operator for floats it '+.' (a plus with a dot after to
denote that it works with floats). So if we try our operation again,
but with +., we get:
# 2 +. 2.5;;
This expression has type int but is here used with type float
Now the 2 is underlined and ocaml complains that an int is being used
with a float operator. So how are we going to make this sum? Well, we
need to convert one of the numbers to the type of the other. If we
convert 2.5 to int, we lose the .5 (which is important), so we'll
convert 2 to be a float.
# (float_of_int 2) +. 2.5;;
- : float = 4.5
What is float_of_int? Let's ask ocaml!
# float_of_int;;
- : int -> float = <fun>
O'caml tells us that float_of_int is a function (<fun>) that takes an
integer argument (int) and returns a float value. So when you gave
float_of_int the argument 2, it returned the same thing, but with type
float so that the operation could be made.
So remember to always work the same type!
List of Arithmetic operators for integers:
+ : Addition
- : Substraction
* : Multiplication
/ : Division
mod : Modulo
List of Arithmetic operators for floats:
+. : Addition
-. : Substraction
*. : Multiplication
/. : Division
** : Exponentiation
To convert an integer to a float: float_of_int n
To convert a float to an integer: int_of_float n
That will be all for now. Next time: variables