📜 ⬆️ ⬇️

Lists and other data structures in Ocaml

Introduction


In addition to the basic data types in Objective Caml, the predefined types include a tuple, list, and record.

Tuple


Values ​​of different types can be grouped together; a tuple can be grouped into a common data structure. Values ​​included in the tuple are separated by a comma. For the construction of a tuple the symbol * is used .
Suppose string * char is a tuple in which the first element is a string and the second character.

# ("Habrahabr", 'A') ;;
-: string * char = "Habrahabr", 'A'
# (100,10.2) ;;
-: int * float = 100,10.2

You can avoid using parentheses (...) in the declaration of a tuple, in principle there is no difference, but with brackets, mine is more readable:
')
# “Habrahabr”, 'A' ;;
-: string * char = "Habrahabr", 'A'

As we see in the above example, without parentheses, the result has not changed.

Functions for working with tuples:
fst - returns the first element of the tuple
snd - returns the second element of the tuple
Both functions are polymorphic, that is, the input argument can be of any type.

#fst ;;
-: 'a' * 'b' -> 'a = #fst (100,10) ;;
-: int = 100

Records


A record is a tuple in which everyone is assigned a name. Entries always correspond to a new type declaration. To define a new record, you must specify its name, as well as the name and type for each field of the record, that is, the syntax for declaring a record is as follows:
type recordName = {field1: dataType; field2: dataType}
where type is a keyword in Objective Caml, recordName is the name of our record, field1, field1 are the fields of our record, dataType is the data type of the record fields. With this basic syntax for describing a record, you can describe any record in Objective Caml:
type complexNum = {real: float; im: float} ;;
type complexNum = {real: float; im: float}
In order to create a record type value, you need to assign a value to each record field:
In the following example, we will create a complex number in which the real part will be 6, and the imaginary 8:
type complexNum = {real: float; im: float} ;;
type complexNum = {real: float; im: float}
let num = {real = 6; im = 8}
val num: complexNum = {real = 6; im = 8}
Access to the record fields can be done in two ways: the dot operator is a field capture, or by matching some fields.
1) expr.field
Here, expr is an expression - a record, field - the required field of the record.
2) {field1 = e1; ... fieldn = en}
Here e1, ..., en are matching patterns.
The advantage of writing before a tuple is a more informative description. thanks to the entry field names.

Lists


The value of the same type can be combined into a data structure - a list . The list in Objective Caml can be either empty or contain elements of the same type.
# [] ;;
-: 'a list = []
# [1; 2; 3] ;;
-: int list = [1; 2; 3]
In order to add a new element to the top of the list, there is the following function defined as an infex operator :
# 1 :: [2; 3] ;;
-: int list = [1; 2; 3]
For combining lists, there exists an infix operator @:
# [1] @ [2; 3] ;;
-: int list = [1; 2; 3]
The remaining functions for working with lists are defined in the List library.

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


All Articles