The difference between a list and an array is that an array is stored as one contiguous block of bytes in memory and a list is 'scattered' around without the elements having to be adjacent to each other. The advantage is that we can use memory more efficiently by filling the memory more flexibly. The downside is that for operations on the whole list, we have to visit each element one by one which may be computationally expensive.
For more on the List data structures visit recursive types.
The convention is to name the module alias after the file name it is defined in:
import List "mo:base/List";
Function nil
Function isNil
Function push
Function last
Function pop
Function size
Function get
Function reverse
Function iterate
Function map
Function filter
Function partition
Function mapFilter
Function mapResult
Function append
Function flatten
Function take
Function drop
Function foldLeft
Function foldRight
Function find
Function some
Function all
Function merge
Function compare
Function equal
Function tabulate
Function make
Function replicate
Function zip
Function zipWith
Function split
Function chunks
Function fromArray
Function fromVarArray
Function toArray
Function toVarArray
Function toIter
func nil<T>() : List<T>
parameters | |
Generic parameters | T |
Return type | List<T> |
import List "mo:base/List";
List.nil<Int>();
func isNil<T>(l : List<T>) : Bool
parameters | |
Generic parameters | T |
Variable argument | l : List<T> |
Return type | Bool |
import List "mo:base/List";
let nil : List.List<Int> = List.nil<Int>();
List.isNil(nil);
func push<T>(
x : T
l : List<T>
) : List<T>
parameters | |
Generic parameters | T |
Variable argument1 | x : T |
Variable argument2 | l : List<T> |
Return type | List<T> |
import List "mo:base/List";
let nil : List.List<Int> = List.nil<Int>();
List.push(-1, nil);
func last<T>(l : List<T>) : ?T
parameters | |
Generic parameters | T |
Variable argument | l : List<T> |
Return type | ?T |
import List "mo:base/List";
let list : List.List<Int> = ?(0, ?(-1, null));
List.last(list);
func pop<T>(l : List<T>) : (?T, List<T>)
parameters | |
Generic parameters | T |
Variable argument | l : List<T> |
Return type | (?T, List<T>) |
import List "mo:base/List";
let list : List.List<Int> = ?(0, ?(-1, null));
List.pop(list);
func size<T>(l : List<T>) : Nat
parameters | |
Generic parameters | T |
Variable argument | l : List<T> |
Return type | Nat |
import List "mo:base/List";
let list : List.List<Int> = ?(1, ?(0, ?(-1, null)));
List.size(list);
func get<T>(
l : List<T>
n : Nat
) : ?T
parameters | |
Generic parameters | T |
Variable argument1 | l : List<T> |
Variable argument2 | n : Nat |
Return type | ?T |
import List "mo:base/List";
let list : List.List<Int> = ?(0, ?(-1, null));
List.get(list, 0);
func reverse<T>(l : List<T>) : List<T>
parameters | |
Generic parameters | T |
Variable argument | l : List<T> |
Return type | List<T> |
import List "mo:base/List";
let list : List.List<Int> = ?(1, ?(0, ?(-1, null)));
List.reverse(list);
func iterate<T>(
l : List<T>
f : T -> ()
) : ()
parameters | |
Generic parameters | T |
Variable argument | l : List<T> |
Function argument | f : T -> () |
Return type | () |
import List "mo:base/List";
import Iter "mo:base/Iter";
let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
var number : Int = 5;
func edit(x : Int) : () {
number += x;
};
let iterate : () = List.iterate(list, edit);
List.toArray(list);
func map<T, U>(
l : List<T>
f : T -> U
) : List<U>
parameters | |
Generic parameters | T, U |
Variable argument | l : List<T> |
Function argument | f : T -> U |
Return type | List<U> |
import List "mo:base/List";
import Int "mo:base/Int";
let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
func change(x : Int) : Text {
Int.toText(x);
};
List.map<Int, Text>(list, change);
func filter<T>(
l : List<T>
f : T -> Bool
) : List<T>
parameters | |
Generic parameters | T |
Variable argument | l : List<T> |
Function argument | f : T -> Bool |
Return type | List<T> |
import List "mo:base/List";
import Int "mo:base/Int";
let list : List.List<Int> = ?(1, ?(0, ?(-1, null)));
func change(x : Int) : Bool {
x > 0;
};
List.filter<Int>(list, change);
func partition<T>(
l : List<T>
f : T -> Bool
) : (List<T>, List<T>)
parameters | |
Generic parameters | T |
Variable argument | l : List<T> |
Function argument | f : T -> Bool |
Return type | (List<T>, List<T>) |
import List "mo:base/List";
import Int "mo:base/Int";
let list : List.List<Int> = ?(1, ?(0, ?(-1, ?(-2, null))));
func change(x : Int) : Bool {
x >= 0;
};
List.partition<Int>(list, change);
func mapFilter<T, U>(
l : List<T>
f : T -> ?U
) : List<U>
parameters | |
Generic parameters | T, U |
Variable argument | l : List<T> |
Function argument | f : T -> ?U |
Return type | List<U> |
import List "mo:base/List";
import Int "mo:base/Int";
let list : List.List<Int> = ?(1, ?(0, ?(-1, null)));
func change(x : Int) : ?Text {
if (x >= 0) {
?(Int.toText(x));
} else {
null;
};
};
List.mapFilter<Int, Text>(list, change);
func mapResult<T, R, E>(
xs : List<T>
f : T -> Result.Result<R, E>
) : Result.Result<List<R>, E>
parameters | |
Generic parameters | T, R, E |
Variable argument | xs : List<T> |
Function argument | f : T -> Result.Result<R, E> |
Return type | Result.Result<List<R>, E> |
import List "mo:base/List";
import Result "mo:base/Result";
let list : List.List<Int> = ?(1, ?(0, ?(-1, null)));
func result(x : Int) : Result.Result<Int, Text> {
if (x >= 0) {
#ok(x);
} else {
#err("it is negative list element");
};
};
List.mapResult<Int, Int, Text>(list, result);
func append<T>(
l : List<T>
m : List<T>
) : (List<T>)
parameters | |
Generic parameters | T |
Variable argument1 | l : List<T> |
Variable argument2 | m : List<T> |
Return type | List<T> |
import List "mo:base/List";
let listN : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
let listP : List.List<Int> = ?(2, ?(1, ?(0, null)));
List.append<Int>(listP, listN);
func flatten<T>(
l : List<List<T>>
) : (List<T>)
parameters | |
Generic parameters | T |
Variable argument | l : List<List<T>> |
Return type | List<T> |
import List "mo:base/List";
let listN : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
let listP : List.List<Int> = ?(2, ?(1, ?(0, null)));
let listOflists : List.List<List.List<Int>> = ?(listN, ?(listP, null));
List.flatten<Int>(listOflists);
func take<T>(
l : List<T>
n : Nat
) : (List<T>)
parameters | |
Generic parameters | T |
Variable argument1 | l : List<T> |
Variable argument2 | n : Nat |
Return type | List<T> |
import List "mo:base/List";
let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
List.take<Int>(list, 2);
func drop<T>(
l : List<T>
n : Nat
) : (List<T>)
parameters | |
Generic parameters | T |
Variable argument1 | l : List<T> |
Variable argument2 | n : Nat |
Return type | List<T> |
import List "mo:base/List";
let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
List.drop<Int>(list, 2);
func foldLeft<T, S>(
list : List<T>
base : S
combine : (S, T) -> S
) : S
parameters | |
Generic parameters | T, S |
Variable argument1 | list : List<T> |
Variable argument2 | base : S |
Function argument | combine : (S, T) -> S |
Return type | S |
import List "mo:base/List";
let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
let base : Int = 10;
func change(x : Int, y : Int) : Int {
x + y;
};
List.foldLeft<Int, Int>(list, 2, change);
func foldRight<T, S>(
list : List<T>
base : S
combine : (T, S) -> S
) : S
parameters | |
Generic parameters | T, S |
Variable argument1 | list : List<T> |
Variable argument2 | base : S |
Function argument | combine : (T, S) -> S |
Return type | S |
import List "mo:base/List";
let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
let base : Int = 10;
func change(x : Int, y : Int) : Int {
x + y;
};
List.foldRight<Int, Int>(list, 2, change);
func find<T>(
l : List<T>
f : T -> Bool
) : ?T
parameters | |
Generic parameters | T |
Variable argument | l : List<T> |
Function argument | f : T -> Bool |
Return type | ?T |
import List "mo:base/List";
let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
let base : Int = 10;
func change(x : Int) : Bool {
x > -2;
};
List.find<Int>(list, change);
func some<T>(
l : List<T>
f : T -> Bool
) : Bool
parameters | |
Generic parameters | T |
Variable argument | l : List<T> |
Function argument | f : T -> Bool |
Return type | Bool |
import List "mo:base/List";
let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
let base : Int = 10;
func change(x : Int) : Bool {
x == -2;
};
List.some<Int>(list, change);
func all<T>(
l : List<T>
f : T -> Bool
) : Bool
parameters | |
Generic parameters | T |
Variable argument | l : List<T> |
Function argument | f : T -> Bool |
Return type | Bool |
import List "mo:base/List";
let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
let base : Int = 10;
func change(x : Int) : Bool {
x > 0;
};
List.all<Int>(list, change);
func merge<T>(
l1 : List<T>
l2 : List<T>
lessThanOrEqual : (T, T) -> Bool
) : List<T>
parameters | |
Generic parameters | T |
Variable argument1 | l1 : List<T> |
Variable argument2 | l2 : List<T> |
Function argument | lessThanOrEqual : (T, T) -> Bool |
Return type | List<T> |
import List "mo:base/List";
let list1 : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
let list2 : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
func change(x : Int, y : Int) : Bool {
x <= y;
};
List.merge<Int>(list1, list2, change);
func compare<T>(
l1 : List<T>
l2 : List<T>
compare : (T, T) -> Order.Order
) : Order.Order
parameters | |
Generic parameters | T |
Variable argument1 | l1 : List<T> |
Variable argument2 | l2 : List<T> |
Function argument | compare : (T, T) -> Order.Order |
Return type | Order.Order |
import List "mo:base/List";
import Int "mo:base/Int";
import Order "mo:base/Order";
let list1 : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
let list2 : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
List.compare<Int>(list1, list2, Int.compare);
func equal<T>(
l1 : List<T>
l2 : List<T>
equal : (T, T) -> Bool
) : Bool
parameters | |
Generic parameters | T |
Variable argument1 | l1 : List<T> |
Variable argument2 | l2 : List<T> |
Function argument | equal : (T, T) -> Bool |
Return type | Bool |
import List "mo:base/List";
import Int "mo:base/Int";
let list1 : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
let list2 : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
List.equal<Int>(list1, list2, Int.equal);
func tabulate<T>(
n : Nat
f : Nat -> T
) : List<T>
parameters | |
Generic parameters | T |
Variable argument | n : Nat |
Function argument | f : Nat -> T |
Return type | List<T> |
import List "mo:base/List";
func change(x : Int) : Int {
x * 2;
};
List.tabulate<Int>(3, change);
func make<T>(n : T) : List<T>
parameters | |
Generic parameters | T |
Variable argument | n : T |
Return type | List<T> |
import List "mo:base/List";
import Int "mo:base/Int";
List.make<Int>(3);
func replicate<T>(
n : Nat
x : T
) : List<T>
parameters | |
Generic parameters | T |
Variable argument1 | n : Nat |
Variable argument2 | x : T |
Return type | List<T> |
import List "mo:base/List";
import Int "mo:base/Int";
List.replicate<Int>(3, 3);
func zip<T, U>(
xs : List<T>
ys : List<U>
) : List<(T, U)>
parameters | |
Generic parameters | T, U |
Variable argument1 | xs : List<T> |
Variable argument2 | ys : List<U> |
Return type | List<(T, U)> |
import List "mo:base/List";
let listN : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
let listP : List.List<Int> = ?(1, ?(0, null));
List.zip<Int, Int>(listN, listP);
func zipWith<T, U, V>(
xs : List<T>
ys : List<U>
f : (T, U) -> V
) : List<V>
parameters | |
Generic parameters | T, U, V |
Variable argument1 | xs : List<T> |
Variable argument2 | ys : List<U> |
Function argument2 | f : (T, U) -> V |
Return type | List<V> |
import List "mo:base/List";
let listN : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
let listP : List.List<Int> = ?(0, ?(0, null));
func edit(x : Int, y : Int) : Int {
x * y;
};
List.zipWith<Int, Int, Int>(listN, listP, edit);
func split<T>(
n : Nat
xs : List<T>
) : (List<T>, List<T>)
parameters | |
Generic parameters | T |
Variable argument1 | n : Nat |
Variable argument2 | xs : List<T> |
Return type | (List<T>, List<T>) |
import List "mo:base/List";
let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
List.split<Int>(2, list);
func chunks<T>(
n : Nat
xs : List<T>
) : List<List<T>>
parameters | |
Generic parameters | T |
Variable argument1 | n : Nat |
Variable argument2 | xs : List<T> |
Return type | List<List<T>> |
import List "mo:base/List";
let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
List.chunks<Int>(1, list);
func fromArray<T>(xs : [T]) : List<T>
parameters | |
Generic parameters | T |
Variable argument | xs : [T] |
Return type | List<T> |
import List "mo:base/List";
let array : [Int] = [-2, -1, 0, 1, 2];
List.fromArray<Int>(array);
func fromVarArray<T>(
xs : [var T]
) : List<T>
parameters | |
Generic parameters | T |
Variable argument | xs : [var T] |
Return type | List<T> |
import List "mo:base/List";
let varArray : [var Int] = [var -2, -1, 0, 1, 2];
List.fromVarArray<Int>(varArray);
func toArray<T>(xs : List<T>) : [T]
parameters | |
Generic parameters | T |
Variable argument | xs : List<T> |
Return type | [T] |
import List "mo:base/List";
let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
List.toArray<Int>(list);
func toVarArray<T>(xs : List<T>) : [var T]
parameters | |
Generic parameters | T |
Variable argument | xs : List<T> |
Return type | [var T] |
import List "mo:base/List";
let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
List.toVarArray<Int>(list);
func toIter<T>(xs : List<T>) : Iter.Iter<T>
parameters | |
Generic parameters | T |
Variable argument | xs : List<T> |
Return type | Iter.Iter<T> |
import List "mo:base/List";
import Iter "mo:base/Iter";
let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));
let iter : Iter.Iter<Int> = List.toIter<Int>(list);
var number : Int = 10;
for (i in iter) {
number += i;
};
number