--- id: list-reference title: List description: List operations hide_table_of_contents: true --- import Syntax from '@theme/Syntax'; import SyntaxTitle from '@theme/SyntaxTitle'; function length : nat val length : nat let length: nat Get the number of elements in a list. function size : nat val size : nat let size: nat Get the number of elements in a list. Synonym for `List.length`. function iter : ('a -> unit) -> list('a) -> unit val iter : ('a -> unit) -> 'a list -> unit let iter: (('a => unit), list('a)) => unit Iterate over items in a list. ```pascaligo group=lists function iter_op (const l : list (int)) : unit is block { function iterated (const i : int) : unit is if i > 3 then Unit else (failwith ("Below range.") : unit) } with List.iter (iterated, l) ``` > Note that `list_iter` is *deprecated*. Alternatively it's also possible to use [loops](../language-basics/loops.md). ```cameligo group=lists let iter_op (l : int list) : unit = let predicate = fun (i : int) -> assert (i > 3) in List.iter predicate l ``` ```reasonligo group=lists let iter_op = (l : list (int)) : unit => { let predicate = (i : int) => assert (i > 3); List.iter (predicate, l); }; ``` function map : ('a -> 'b) -> list('a) -> list('b) val map : ('a -> 'b) -> 'a list -> 'b list let map: (('a => 'b), list('a)) => list('b) Apply a function to items of a list to create a new list. ```pascaligo group=lists const larger_list: list(int) = list [1; 2; 3] function increment (const i : int): int is i + 1 // Creates a new list with all elements incremented by 1 const plus_one : list (int) = List.map (increment, larger_list) ``` > Note that `list_map` is *deprecated*. ```cameligo group=lists let larger_list: int list = [1; 2; 3] 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 ``` ```reasonligo group=lists let larger_list: list(int) = [1, 2, 3]; 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); ``` function fold : (('accumulator -> 'item -> 'accumulator) -> list('item) -> 'accumulator) -> 'accumulator val fold : ('accumulator -> 'item -> 'accumulator) -> 'item list -> 'accumulator -> 'accumulator let fold: ((('accumulator, 'item) => 'accumulator), list('item), 'accumulator) => 'accumulator [Fold over items in a list](../language-basics/sets-lists-tuples.md#folded-operation-over-lists); ```pascaligo group=lists const my_list: list(int) = list [1; 2; 3] function sum (const acc : int; const i : int): int is acc + i const sum_of_elements : int = List.fold (sum, my_list, 0) ``` > Note that `list_fold` is *deprecated*. ```cameligo group=lists let my_list : int list = [1; 2; 3] let sum (acc, i : int * int) : int = acc + i let sum_of_elements : int = List.fold sum my_list 0 ``` ```reasonligo group=lists let my_list : list(int) = [1, 2, 3]; let sum = ((result, i): (int, int)): int => result + i; let sum_of_elements : int = List.fold (sum, my_list, 0); ```