2020-02-08 08:17:13 +04:00
|
|
|
---
|
|
|
|
id: list-reference
|
2020-02-26 21:31:58 +04:00
|
|
|
title: Lists — Linear Collections
|
2020-02-08 08:17:13 +04:00
|
|
|
---
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
import Syntax from '@theme/Syntax';
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
Lists are linear collections of elements of the same type. Linear
|
|
|
|
means that, in order to reach an element in a list, we must visit all
|
|
|
|
the elements before (sequential access). Elements can be repeated, as
|
|
|
|
only their order in the collection matters. The first element is
|
|
|
|
called the *head*, and the sub-list after the head is called the
|
|
|
|
*tail*. For those familiar with algorithmic data structure, you can
|
|
|
|
think of a list a *stack*, where the top is written on the left.
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
# Defining Lists
|
2020-02-25 21:07:53 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
```pascaligo group=lists
|
|
|
|
const empty_list : list (int) = nil // Or list []
|
|
|
|
const my_list : list (int) = list [1; 2; 2] // The head is 1
|
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
```cameligo group=lists
|
|
|
|
let empty_list : int list = []
|
|
|
|
let my_list : int list = [1; 2; 2] // The head is 1
|
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
```reasonligo group=lists
|
|
|
|
let empty_list : list (int) = [];
|
|
|
|
let my_list : list (int) = [1, 2, 2]; // The head is 1
|
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
# Adding to Lists
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
Lists can be augmented by adding an element before the head (or, in
|
|
|
|
terms of stack, by *pushing an element on top*).
|
|
|
|
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
```pascaligo group=lists
|
|
|
|
const larger_list : list (int) = 5 # my_list // [5;1;2;2]
|
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
```cameligo group=lists
|
|
|
|
let larger_list : int list = 5 :: my_list // [5;1;2;2]
|
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
```reasonligo group=lists
|
|
|
|
let larger_list : list (int) = [5, ...my_list]; // [5,1,2,2]
|
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
# Functional Iteration over Lists
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
A *functional iterator* is a function that traverses a data structure
|
|
|
|
and calls in turn a given function over the elements of that structure
|
|
|
|
to compute some value. Another approach is possible in PascaLIGO:
|
|
|
|
*loops* (see the relevant section).
|
|
|
|
|
|
|
|
There are three kinds of functional iterations over LIGO lists: the
|
|
|
|
*iterated operation*, the *map operation* (not to be confused with the
|
|
|
|
*map data structure*) and the *fold operation*.
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
## Iterated Operation over Lists
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
The first, the *iterated operation*, is an iteration over the list
|
|
|
|
with a unit return value. It is useful to enforce certain invariants
|
|
|
|
on the element of a list, or fail.
|
|
|
|
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
```pascaligo group=lists
|
|
|
|
function iter_op (const l : list (int)) : unit is
|
|
|
|
block {
|
|
|
|
function iterated (const i : int) : unit is
|
2020-02-26 21:31:58 +04:00
|
|
|
if i > 3 then Unit else (failwith ("Below range.") : unit)
|
|
|
|
} with List.iter (iterated, l)
|
2020-02-25 21:07:53 +04:00
|
|
|
```
|
2020-02-26 21:31:58 +04:00
|
|
|
|
|
|
|
> Note that `list_iter` is *deprecated*.
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
```cameligo group=lists
|
|
|
|
let iter_op (l : int list) : unit =
|
|
|
|
let predicate = fun (i : int) -> assert (i > 3)
|
|
|
|
in List.iter predicate l
|
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
```reasonligo group=lists
|
|
|
|
let iter_op = (l : list (int)) : unit => {
|
|
|
|
let predicate = (i : int) => assert (i > 3);
|
|
|
|
List.iter (predicate, l);
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
## Mapped Operation over Lists
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
We may want to change all the elements of a given list by applying to
|
|
|
|
them a function. This is called a *map operation*, not to be confused
|
|
|
|
with the map data structure.
|
|
|
|
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
```pascaligo group=lists
|
|
|
|
function increment (const i : int): int is i + 1
|
|
|
|
|
|
|
|
// Creates a new list with all elements incremented by 1
|
2020-02-26 21:31:58 +04:00
|
|
|
const plus_one : list (int) = List.map (increment, larger_list)
|
2020-02-25 21:07:53 +04:00
|
|
|
```
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
> Note that `list_map` is *deprecated*.
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
```cameligo group=lists
|
|
|
|
let increment (i : int) : int = i + 1
|
|
|
|
|
|
|
|
// Creates a new list with all elements incremented by 1
|
|
|
|
let plus_one : int list = List.map increment larger_list
|
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
```reasonligo group=lists
|
|
|
|
let increment = (i : int) : int => i + 1;
|
|
|
|
|
|
|
|
// Creates a new list with all elements incremented by 1
|
|
|
|
let plus_one : list (int) = List.map (increment, larger_list);
|
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
|
|
|
|
## Folded Operation over Lists
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
A *folded operation* is the most general of iterations. The folded
|
|
|
|
function takes two arguments: an *accumulator* and the structure
|
|
|
|
*element* at hand, with which it then produces a new accumulator. This
|
|
|
|
enables having a partial result that becomes complete when the
|
|
|
|
traversal of the data structure is over.
|
|
|
|
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
```pascaligo group=lists
|
|
|
|
function sum (const acc : int; const i : int): int is acc + i
|
2020-02-26 21:31:58 +04:00
|
|
|
const sum_of_elements : int = List.fold (sum, my_list, 0)
|
2020-02-25 21:07:53 +04:00
|
|
|
```
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
> Note that `list_fold` is *deprecated*.
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
```cameligo group=lists
|
|
|
|
let sum (acc, i: int * int) : int = acc + i
|
|
|
|
let sum_of_elements : int = List.fold sum my_list 0
|
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
```reasonligo group=lists
|
|
|
|
let sum = ((result, i): (int, int)): int => result + i;
|
|
|
|
let sum_of_elements : int = List.fold (sum, my_list, 0);
|
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
# List Length
|
2020-02-08 08:17:13 +04:00
|
|
|
|
|
|
|
Get the number of elements in a list.
|
|
|
|
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-08 08:17:13 +04:00
|
|
|
```pascaligo
|
2020-02-25 21:07:53 +04:00
|
|
|
function size_of (const l : list (int)) : nat is List.length (l)
|
2020-02-08 08:17:13 +04:00
|
|
|
```
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
> Note that `size` is *deprecated*.
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-08 08:17:13 +04:00
|
|
|
```cameligo
|
2020-02-25 21:07:53 +04:00
|
|
|
let size_of (l : int list) : nat = List.length l
|
2020-02-08 08:17:13 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-08 08:17:13 +04:00
|
|
|
```reasonligo
|
2020-02-25 21:07:53 +04:00
|
|
|
let size_of = (l : list (int)) : nat => List.length (l);
|
2020-02-08 08:17:13 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
|