List

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";

On this page

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

List.nil

func nil<T>() : List<T>
parameters
Generic parametersT
Return typeList<T>

Example

import List "mo:base/List";

List.nil<Int>();

List.isNil

func isNil<T>(l : List<T>) : Bool
parameters
Generic parametersT
Variable argumentl : List<T>
Return typeBool

Example

import List "mo:base/List";

let nil : List.List<Int> = List.nil<Int>();

List.isNil(nil);

List.push

func push<T>(

  x : T
  l : List<T>

) : List<T>
parameters
Generic parametersT
Variable argument1x : T
Variable argument2l : List<T>
Return typeList<T>

Example

import List "mo:base/List";

let nil : List.List<Int> = List.nil<Int>();

List.push(-1, nil);

List.last

func last<T>(l : List<T>) : ?T
parameters
Generic parametersT
Variable argumentl : List<T>
Return type?T

Example

import List "mo:base/List";

let list : List.List<Int> = ?(0, ?(-1, null));

List.last(list);

List.pop

func pop<T>(l : List<T>) : (?T, List<T>)
parameters
Generic parametersT
Variable argumentl : List<T>
Return type(?T, List<T>)

Example

import List "mo:base/List";

let list : List.List<Int> = ?(0, ?(-1, null));

List.pop(list);

List.size

func size<T>(l : List<T>) : Nat
parameters
Generic parametersT
Variable argumentl : List<T>
Return typeNat

Example

import List "mo:base/List";

let list : List.List<Int> = ?(1, ?(0, ?(-1, null)));

List.size(list);

List.get

func get<T>(

  l : List<T>
  n : Nat

) : ?T
parameters
Generic parametersT
Variable argument1l : List<T>
Variable argument2n : Nat
Return type?T

Example

import List "mo:base/List";

let list : List.List<Int> = ?(0, ?(-1, null));

List.get(list, 0);

List.reverse

func reverse<T>(l : List<T>) : List<T>
parameters
Generic parametersT
Variable argumentl : List<T>
Return typeList<T>

Example

import List "mo:base/List";

let list : List.List<Int> = ?(1, ?(0, ?(-1, null)));

List.reverse(list);

List.iterate

func iterate<T>(

  l : List<T>
  f : T -> ()

) : ()
parameters
Generic parametersT
Variable argumentl : List<T>
Function argumentf : T -> ()
Return type()

Example

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);

List.map

func map<T, U>(

  l : List<T>
  f : T -> U

) : List<U>
parameters
Generic parametersT, U
Variable argumentl : List<T>
Function argumentf : T -> U
Return typeList<U>

Example

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);

List.filter

func filter<T>(

  l : List<T>
  f : T -> Bool

) : List<T>
parameters
Generic parametersT
Variable argumentl : List<T>
Function argumentf : T -> Bool
Return typeList<T>

Example

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);

List.partition

func partition<T>(

  l : List<T>
  f : T -> Bool

) : (List<T>, List<T>)
parameters
Generic parametersT
Variable argumentl : List<T>
Function argumentf : T -> Bool
Return type(List<T>, List<T>)

Example

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);

List.mapFilter

func mapFilter<T, U>(

  l : List<T>
  f : T -> ?U

) : List<U>
parameters
Generic parametersT, U
Variable argumentl : List<T>
Function argumentf : T -> ?U
Return typeList<U>

Example

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);

List.mapResult

func mapResult<T, R, E>(

  xs : List<T>
   f : T -> Result.Result<R, E>

) : Result.Result<List<R>, E>
parameters
Generic parametersT, R, E
Variable argumentxs : List<T>
Function argumentf : T -> Result.Result<R, E>
Return typeResult.Result<List<R>, E>

Example

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);

List.append

func append<T>(

  l : List<T>
  m : List<T>

) : (List<T>)
parameters
Generic parametersT
Variable argument1l : List<T>
Variable argument2m : List<T>
Return typeList<T>

Example

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);

List.flatten

func flatten<T>(

l : List<List<T>>

) : (List<T>)
parameters
Generic parametersT
Variable argumentl : List<List<T>>
Return typeList<T>

Example

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);

List.take

func take<T>(

  l : List<T>
  n : Nat

) : (List<T>)
parameters
Generic parametersT
Variable argument1l : List<T>
Variable argument2n : Nat
Return typeList<T>

Example

import List "mo:base/List";

let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));

List.take<Int>(list, 2);

List.drop

func drop<T>(

  l : List<T>
  n : Nat

) : (List<T>)
parameters
Generic parametersT
Variable argument1l : List<T>
Variable argument2n : Nat
Return typeList<T>

Example

import List "mo:base/List";

let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));

List.drop<Int>(list, 2);

List.foldLeft

func foldLeft<T, S>(

    list : List<T>
    base : S
 combine : (S, T) -> S

) : S
parameters
Generic parametersT, S
Variable argument1list : List<T>
Variable argument2base : S
Function argumentcombine : (S, T) -> S
Return typeS

Example

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);

List.foldRight

func foldRight<T, S>(

    list : List<T>
    base : S
 combine : (T, S) -> S

) : S
parameters
Generic parametersT, S
Variable argument1list : List<T>
Variable argument2base : S
Function argumentcombine : (T, S) -> S
Return typeS

Example

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);

List.find

func find<T>(

  l : List<T>
  f : T -> Bool

) : ?T
parameters
Generic parametersT
Variable argumentl : List<T>
Function argumentf : T -> Bool
Return type?T

Example

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);

List.some

func some<T>(

  l : List<T>
  f : T -> Bool

) : Bool
parameters
Generic parametersT
Variable argumentl : List<T>
Function argumentf : T -> Bool
Return typeBool

Example

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);

List.all

func all<T>(

  l : List<T>
  f : T -> Bool

) : Bool
parameters
Generic parametersT
Variable argumentl : List<T>
Function argumentf : T -> Bool
Return typeBool

Example

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);

List.merge

func merge<T>(

    l1 : List<T>
    l2 : List<T>
    lessThanOrEqual : (T, T) -> Bool

) : List<T>
parameters
Generic parametersT
Variable argument1l1 : List<T>
Variable argument2l2 : List<T>
Function argumentlessThanOrEqual : (T, T) -> Bool
Return typeList<T>

Example

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);

List.compare

func compare<T>(

     l1 : List<T>
     l2 : List<T>
compare : (T, T) -> Order.Order

) : Order.Order
parameters
Generic parametersT
Variable argument1l1 : List<T>
Variable argument2l2 : List<T>
Function argumentcompare : (T, T) -> Order.Order
Return typeOrder.Order

Example

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);

List.equal

func equal<T>(

   l1 : List<T>
   l2 : List<T>
equal : (T, T) -> Bool

) : Bool
parameters
Generic parametersT
Variable argument1l1 : List<T>
Variable argument2l2 : List<T>
Function argumentequal : (T, T) -> Bool
Return typeBool

Example

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);

List.tabulate

func tabulate<T>(

  n : Nat
  f : Nat -> T

) : List<T>
parameters
Generic parametersT
Variable argumentn : Nat
Function argumentf : Nat -> T
Return typeList<T>

Example

import List "mo:base/List";

func change(x : Int) : Int {
    x * 2;
};

List.tabulate<Int>(3, change);

List.make

func make<T>(n : T) : List<T>
parameters
Generic parametersT
Variable argumentn : T
Return typeList<T>

Example

import List "mo:base/List";
import Int "mo:base/Int";

List.make<Int>(3);

List.replicate

func replicate<T>(

  n : Nat
  x : T

) : List<T>
parameters
Generic parametersT
Variable argument1n : Nat
Variable argument2x : T
Return typeList<T>

Example

import List "mo:base/List";
import Int "mo:base/Int";

List.replicate<Int>(3, 3);

List.zip

func zip<T, U>(

  xs : List<T>
  ys : List<U>

) : List<(T, U)>
parameters
Generic parametersT, U
Variable argument1xs : List<T>
Variable argument2ys : List<U>
Return typeList<(T, U)>

Example

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);

List.zipWith

func zipWith<T, U, V>(

  xs : List<T>
  ys : List<U>
   f : (T, U) -> V

) : List<V>
parameters
Generic parametersT, U, V
Variable argument1xs : List<T>
Variable argument2ys : List<U>
Function argument2f : (T, U) -> V
Return typeList<V>

Example

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);

List.split

func split<T>(

  n : Nat
 xs : List<T>

) : (List<T>, List<T>)
parameters
Generic parametersT
Variable argument1n : Nat
Variable argument2xs : List<T>
Return type(List<T>, List<T>)

Example

import List "mo:base/List";

let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));

List.split<Int>(2, list);

List.chunks

func chunks<T>(

  n : Nat
 xs : List<T>

) : List<List<T>>
parameters
Generic parametersT
Variable argument1n : Nat
Variable argument2xs : List<T>
Return typeList<List<T>>

Example

import List "mo:base/List";

let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));

List.chunks<Int>(1, list);

List.fromArray

func fromArray<T>(xs : [T]) : List<T>
parameters
Generic parametersT
Variable argumentxs : [T]
Return typeList<T>

Example

import List "mo:base/List";

let array : [Int] = [-2, -1, 0, 1, 2];

List.fromArray<Int>(array);

List.fromVarArray

func fromVarArray<T>(

 xs : [var T]

) : List<T>
parameters
Generic parametersT
Variable argumentxs : [var T]
Return typeList<T>

Example

import List "mo:base/List";

let varArray : [var Int] = [var -2, -1, 0, 1, 2];

List.fromVarArray<Int>(varArray);

List.toArray

func toArray<T>(xs : List<T>) : [T]
parameters
Generic parametersT
Variable argumentxs : List<T>
Return type[T]

Example

import List "mo:base/List";

let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));

List.toArray<Int>(list);

List.toVarArray

func toVarArray<T>(xs : List<T>) : [var T]
parameters
Generic parametersT
Variable argumentxs : List<T>
Return type[var T]

Example

import List "mo:base/List";

let list : List.List<Int> = ?(-3, ?(-2, ?(-1, null)));

List.toVarArray<Int>(list);

List.toIter

func toIter<T>(xs : List<T>) : Iter.Iter<T>
parameters
Generic parametersT
Variable argumentxs : List<T>
Return typeIter.Iter<T>

Example

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