📜 ⬆️ ⬇️

Ocaml. Data types

Introduction


In this post we turn directly to the introduction of the Objective Caml language. This post will cover the basic data types of Objective Caml.
First you need to download and install Objective Caml, at this stage one interpreter will be enough. The interpreter is started using the: ocaml command, if it happens in * nix, or running ocaml.exe if it happens in Windows.
After running the interpreter, we will see the following: the Ocaml version, I have this Objective Caml version 3.00, and the wait for entering commands: #.
Each logical unit of code - a phrase, ends in Objective Caml - ;; The exit from the interpreter is carried out either by pressing Ctrl + D, or after calling the exit function of type int -> int:
exit 0 ;;

Data types


The following basic data types are defined in Objective Caml: integer type, floating-point number, character type, string type, and logical type.
Integer type
Integer numbers in Objective Caml are declared using the int keyword:
# 0 ;;
-: int = 0
# 10 + 10 ;;
-: int = 20
# 10/5 ;;
-: int = 2

Operations on integer type:
+ - addition
- - subtraction and unary minus
* - multiplication
/ - division
mod - the remainder of an integer division

Floating point numbers
Objective Caml floating point numbers are declared using the float keyword:
# 1.0
-: float = 1.0
# 123.1 - 123.-0 / 5.0
-: float = 0.2
#ceil 3.6
-: float = 4
')
Floating-point operations:
+ - addition
- - subtraction and unary minus
* - multiplication
/ - division
** - exponentiation

Basic functions on floating point numbers:
* ceil - round up
* floor - round to the least
* sqrt - square root
* exp - exponent
* log - natural logarithm
* log10 - base 10 logarithm
* sin - sine
* cos - cosine
* tan - tangent
* acos - arccosine
* asin - arcsine
* atan - arctangent

Variables of different types, you can clearly hello to each other, through the data type translation function: float_of_int and int_of_float .

Character type and string
Characters in Objective Caml type char , correspond to integers in the range from 0 to 255, the first 128 values ​​correspond to ASCII,
Strings in Objective Caml - type string - a sequence of characters of a certain length.
# 'A'
-: char = 'A'
int_of_char 'A'
-: int = 65
# "OCAML string"
-: string = "OCAML string"
Translation functions from integer to character type and back: int_of_char and char_of_int .
Translation functions from integer type to string type and back: int_of_string and string_of_int .

Boolean type
A boolean type in Objective Caml is represented by a set consisting of 2 elements: true and false
#true ;;
-: bool = true
#not true ;;
-: bool = false
Logical operations:
< - less
> - more
<= - less than or equal to
> = - greater than or equal to
= - equals
== - physical equality
<> - denial
! = - denial ==
Logical operators:
not - denial
&& - consistent and
|| - sequential or

Unit type
The Unit type defines a set of just one element whose value ():
# () ;;
-: unit = ()
This type is analogous to the void type in the C ++ language.

That's all the basic types of language Ocaml

Source: https://habr.com/ru/post/74574/


All Articles