lists(3)

MODULE
lists - List Processing Functions

DESCRIPTION

This module contains functions for list processing. The functions are organized in two groups: those in the first group perform a particular operation on one or more lists, whereas those in the second group are higher-order functions, using a fun as argument to perform an operation on one list.

EXPORTS

append(ListOfLists) -> List1

    Types
        ListOfLists = [List]
        List = List1 = [term()]

    Returns a list in which all the sub-lists of ListOfLists have been appended. For example:
    >lists:append([[1, 2, 3], [a, b], [4, 5, 6]]).
    [1,2,3,a,b,4,5,6]

append(List1, List2) -> List3

    Types
        List1 = List2 = List3 = [term()]

    Returns a new list List3 which is made from the elements of List1 followed by the elements of List2. For example:
    >lists:append("abc", "def").
    "abcdef"

    lists:append(A, B) is equivalent to A ++ B.

concat(Things) -> string()

    Types
        Things = [Thing]
        Thing = atom() | integer() | float() | string()

    Concatenates the text representation of the elements of Things. The elements of Things can be atoms, integers, floats or strings.
    >lists:concat([doc, '/', file, '.', 3]).
    "doc/file.3"

delete(Elem, List1) -> List2

    Types
        Elem = term()
        List1 = List2 = [term()]

    Returns a copy of List1, but the first occurrence of Elem, if present, is deleted.

duplicate(N, Elem) -> List

    Types
        N = int()
        Elem = term()
        List = [Elem]

    Returns a list which contains N copies of the term Elem. For example:
    >lists:duplicate(5, xx).
    [xx,xx,xx,xx,xx]

flatlength(DeepList) -> int()

    Types
        DeepList = [term() | DeepList]

    Equivalent to length(flatten(DeepList)), but more efficient.

flatten(DeepList) -> List

    Types
        DeepList = [Elem | DeepList]
        Elem = term()
        List = [Elem]

    Returns a flattened version of DeepList.

flatten(DeepList, Tail) -> List

    Types
        DeepList = [Elem | DeepList]
        Elem = term()
        Tail = [term()]
        List = [Elem]

    Returns a flattened version of DeepList with the tail Tail appended.

keydelete(Key, N, TupleList1) -> TupleList2

    Types
        Key = term()
        N = 1..size(Tuple)
        TupleList1 = TupleList2 = [Tuple]
        Tuple = tuple()

    Returns a copy of TupleList1 where the first occurrence of a tuple whose Nth element is Key is deleted, if present.

keymember(Key, N, TupleList) -> bool()

    Types
        Key = term()
        N = 1..size(Tuple)
        TupleList = [Tuple]
        Tuple = tuple()

    Returns true if there is a tuple in TupleList whose Nth element is Key, otherwise false.

keymerge(N, TupleList1, TupleList2) -> TupleList3

    Types
        N = 1..size(Tuple)
        TupleList1 = TupleList2 = TupleList3 = [Tuple]
        Tuple = tuple()

    Returns the sorted list formed by merging TupleList1 and TupleList2. The sorting is performed on the Nth element of each tuple. Both TupleList1 and TupleList2 must be key-sorted prior to evaluating this function. When two keys are equal, elements from TupleList1 are picked before elements from TupleList2.

keyreplace(Key, N, TupleList1, NewTuple) -> TupleList2

    Types
        Key = term()
        N = 1..size(Tuple)
        TupleList1 = TupleList2 = [Tuple]
        NewTuple = Tuple = tuple()

    Returns a copy of TupleList1, where the first occurrence of a tuple whose Nth element is Key, if present, is replaced with NewTuple.

keysearch(Key, N, TupleList) -> {value, Tuple} | false

    Types
        Key = term()
        N = 1..size(Tuple)
        TupleList = [Tuple]
        Tuple = tuple()

    Searches the list of the tuples TupleList for a tuple whose Nth element is Key. Returns {value, Tuple} if such a tuple is found, or false otherwise.

keysort(N, TupleList1) -> TupleList2

    Types
        N = 1..size(Tuple)
        TupleList1 = TupleList2 = [Tuple]
        Tuple = tuple()

    Returns a list containing the sorted elements of TupleList1. Sorting is performed on the Nth element of the tuples.

last(List) -> Last

    Types
        List = [term()]
        Last = term()

    Returns the last element in List.

max(List) -> Max

    Types
        List = [term()]
        Max = term()

    Returns the maximum element of List.

member(Elem, List) -> bool()

    Types
        Elem = term()
        List = [term()]

    Returns true if Elem is an element of List, otherwise false.

merge(ListOfLists) -> List1

    Types
        ListOfLists = [List]
        List = List1 = [term()]

    Returns the sorted list formed by merging all the sub-lists of ListOfLists. All sub-lists must be sorted prior to evaluating this function.

merge(List1, List2) -> List3

    Types
        List1 = List2 = List3 = [term()]

    Returns the sorted list formed by merging List1 and List2. Both List1 and List2 must be sorted prior to evaluating this function.

merge(Fun, List1, List2) -> List3

    Types
        Fun = fun(A, B) -> bool()
        List1 = [A]
        List2 = [B]
        List3 = [A | B]
        A = B = term()

    Returns the sorted list formed by merging List1 and List2. Both List1 and List2 must be sorted according to the ordering function Fun prior to evaluating this function. Fun(A, B) should return true if A comes before B in the ordering, false otherwise.

merge3(List1, List2, List3) -> List4

    Types
        List1 = List2 = List3 = List4 = [term()]

    Returns the sorted list formed by merging List1, List2 and List3. All of List1, List2 and List3 must be sorted prior to evaluating this function.

min(List) -> Min

    Types
        List = [term()]
        Min = term()

    Returns the minimum element of List.

nth(N, List) -> Elem

    Types
        N = int()
        List = [term()]
        Elem = term()

    Returns the Nth element of List. For example:
    >lists:nth(3, [a, b, c, d, e]).
    c

nthtail(N, List1) -> Tail

    Types
        N = int()
        List1 = Tail = [term()]

    Returns the Nth tail of List. For example:
    >lists:nthtail(3, [a, b, c, d, e]).
    [d,e]

prefix(List1, List2) -> bool()

    Types
        List1 = List2 = [term()]

    Returns true if List1 is a prefix of List2, otherwise false.

reverse(List1) -> List2

    Types
        List1 = List2 = [term()]

    Returns a list with the top level elements in List1 in reverse order.

reverse(List1, Tail) -> List2

    Types
        List1 = Tail = List2 = [term()]

    Returns a list with the top level elements in List1 in reverse order, with the tail Tail appended. For example:
    >lists:reverse([1, 2, 3, 4], [a, b, c]).
    [4,3,2,1,a,b,c]

seq(From, To) -> Seq
seq(From, To, Incr) -> Seq

    Types
        From = To = Incr = int()
        Seq = [int()]

    Returns a sequence of integers which starts with From and contains the successive results of adding Incr to the previous element, until To has been reached or passed (in the latter case, To is not an element of the sequence). Incr defaults to 1.

    Limitations: A failure will occur if To < From and Incr is positive, or if To > From and Incr is negative, or if Incr == 0 and From /= To.

    Examples:
    >lists:seq(1, 10).
    [1,2,3,4,5,6,7,8,9,10]
    >lists:seq(1, 20, 3).
    [1,4,7,10,13,16,19]
    >lists:seq(1, 1, 0).
    [1]

sort(List1) -> List2

    Types
        List1 = List2 = [term()]

    Returns a list containing the sorted elements of List1.

sort(Fun, List1) -> List2

    Types
        Fun = fun(Elem1, Elem2) -> bool()
        Elem1 = Elem2 = term()
        List1 = List2 = [term()]

    Returns a list containing the sorted elements of List1, according to the ordering function Fun. Fun(A, B) should return true if A comes before B in the ordering, false otherwise.

split(N, List1) -> {List2, List3}

    Types
        N = 1..length(List1)
        List1 = List2 = List3 = [term()]

    Splits List1 into List2 and List3. List2 contains the first N elements and List3 the rest of the elements.

sublist(List1, N) -> List2

    Types
        List1 = List2 = [term()]
        N = int()

    Returns the first N elements of List1. It is not an error for N to exceed the length of the list - in that case the whole list is returned.

sublist(List1, Start, Length) -> List2

    Types
        List1 = List2 = [term()]
        Start = Length = int()

    Returns the sub-list of List1 starting at Start and of length Length. It is not an error if Start > length(List1) or if (Start+Length) > length(List1.
    >lists:sublist([1,2,3,4], 2, 2).
    [2,3]
    >lists:sublist([1,2,3,4], 2, 5).
    [2,3,4]
    >lists:sublist([1,2,3,4], 5, 2).
    []

subtract(List1, List2) -> List3

    Types
        List1 = List2 = List3 = [term()]

    Returns a new list List3 which is a copy of List1, subjected to the following procedure: for each element in List2, its first occurrence in List1 is removed. For example:
    >lists:subtract("123212", "212").
    "312".

    lists:subtract(A, B) is equivalent to A -- B.

suffix(List1, List2) -> bool()

    Returns true if List1 is a suffix of List2, otherwise false.

sum(List) -> number()

    Types
        List = [number()]

    Returns the sum of the elements in List.

ukeymerge(N, TupleList1, TupleList2) -> TupleList3

    Types
        N = 1..size(Tuple)
        TupleList1 = TupleList2 = TupleList3 = [Tuple]
        Tuple = tuple()

    Returns the sorted list formed by merging TupleList1 and TupleList2 while removing consecutive duplicates. The sorting is performed on the Nth element of each tuple. Both TupleList1 and TupleList2 must be key-sorted and contain no duplicates prior to evaluating this function. When two keys are equal, elements from TupleList1 are picked before elements from TupleList2.

ukeysort(N, TupleList1) -> TupleList2

    Types
        N = 1..size(Tuple)
        TupleList1 = TupleList2 = [Tuple]
        Tuple = tuple()

    Returns a list containing the sorted elements of TupleList1 with consecutive duplicates removed. Sorting is performed on the Nth element of the tuples.

umerge(ListOfLists) -> List1

    Types
        ListOfLists = [List]
        List = List1 = [term()]

    Returns the sorted list formed by merging all the sub-lists of ListOfLists while removing duplicates. All sub-lists must be sorted and contain no duplicates prior to evaluating this function.

umerge(List1, List2) -> List3

    Types
        List1 = List2 = List3 = [term()]

    Returns the sorted list formed by merging List1 and List2 while removing duplicates. Both List1 and List2 must be sorted and contain no duplicates prior to evaluating this function.

umerge(Fun, List1, List2) -> List3

    Types
        Fun = fun(A, B) -> bool()
        List1 = [A]
        List2 = [B]
        List3 = [A | B]
        A = B = term()

    Returns the sorted list formed by merging List1 and List2 while removing consecutive duplicates. Both List1 and List2 must be sorted according to the ordering function Fun and contain no duplicates prior to evaluating this function. Fun(A, B) should return true if A equals or comes before B in the ordering, false otherwise.

umerge3(List1, List2, List3) -> List4

    Types
        List1 = List2 = List3 = List4 = [term()]

    Returns the sorted list formed by merging List1, List2 and List3 while removing duplicates. All of List1, List2 and List3 must be sorted and contain no duplicates prior to evaluating this function.

unzip(List1) -> {List2, List3}

    Types
        List1 = [{X, Y}]
        List2 = [X]
        List3 = [Y]
        X = Y = term()

    "Unzips" a list of two-tuples into two lists, where the first list contains the first element of each tuple, and the second list contains the second element of each tuple.

unzip3(List1) -> {List2, List3, List4}

    Types
        List1 = [{X, Y, Z}]
        List2 = [X]
        List3 = [Y]
        List4 = [Z]
        X = Y = Z = term()

    "Unzips" a list of three-tuples into three lists, where the first list contains the first element of each tuple, the second list contains the second element of each tuple, and the third list contains the third element of each tuple.

usort(List1) -> List2

    Types
        List1 = List2 = [term()]

    Returns a list containing the sorted elements of List1 without duplicates.

usort(Fun, List1) -> List2

    Types
        Fun = fun(Elem1, Elem2) -> bool()
        Elem1 = Elem2 = term()
        List1 = List2 = [term()]

    Returns a list containing the sorted elements of List1 with consecutive duplicates removed, according to the ordering function Fun. Fun(A, B) should return true if A equals or comes before B in the ordering, false otherwise.

zip(List1, List2) -> List3

    Types
        List1 = [X]
        List2 = [Y]
        List3 = [{X, Y}]
        X = Y = term()

    "Zips" two lists of equal length into one list of two-tuples, where the first element of each tuple is taken from the first list and the second element is taken from corresponding element in the second list.

zip3(List1, List2, List3) -> List4

    Types
        List1 = [X]
        List2 = [Y]
        List3 = [Z]
        List3 = [{X, Y, Z}]
        X = Y = Z = term()

    "Zips" three lists of equal length into one list of three-tuples, where the first element of each tuple is taken from the first list, the second element is taken from corresponding element in the second list, and the third element is taken from the corresponding element in the third list.

zipwith(Combine, List1, List2) -> List3

    Types
        Combine = fun(X, Y) -> T
        List1 = [X]
        List2 = [Y]
        List3 = [T]
        X = Y = T = term()

    Combine the elements of two lists of equal length into one list. For each pair X, Y of list elements from the two lists, the element in the result list will be Combine(X, Y).

    zipwith(fun(X, Y) -> {X, Y} end, List1, List2) is equivalent to zip(List1, List2).

    Examples:
    >lists:zipwith(fun(X, Y) -> X+Y end, [1,2,3], [4,5,6]).
    [5,7,9]

zipwith3(Combine, List1, List2, List3) -> List4

    Types
        Combine = fun(X, Y, Z) -> T
        List1 = [X]
        List2 = [Y]
        List3 = [Z]
        List4 = [T]
        X = Y = Z = T = term()

    Combine the elements of three lists of equal length into one list. For each triple X, Y, Z of list elements from the three lists, the element in the result list will be Combine(X, Y, Z).

    zipwith3(fun(X, Y, Z) -> {X, Y, Z} end, List1, List2, List3) is equivalent to zip3(List1, List2, List3).

    Examples:
    >lists:zipwith3(fun(X, Y, Z) -> X+Y+Z end, [1,2,3], [4,5,6], [7,8,9]).
    [12,15,18]
    >lists:zipwith3(fun(X, Y, Z) -> [X,Y,Z] end, [a,b,c], [x,y,z], [1,2,3]).
    [[a,x,1],[b,y,2],[c,z,3]]

all(Pred, List) -> bool()

    Types
        Pred = fun(Elem) -> bool()
        Elem = term()
        List = [term()]

    Returns true if Pred(Elem) returns true for all elements Elem in List, otherwise false.

any(Pred, List) -> bool()

    Types
        Pred = fun(Elem) -> bool()
        Elem = term()
        List = [term()]

    Returns true if Pred(Elem) returns true for at least one element Elem in List.

dropwhile(Pred, List1) -> List2

    Types
        Pred = fun(Elem) -> bool()
        Elem = term()
        List1 = List2 = [term()]

    Drops elements Elem from List1 while Pred(Elem) returns true and returns the remaining list.

filter(Pred, List1) -> List2

    Types
        Pred = fun(Elem) -> bool()
        Elem = term()
        List1 = List2 = [term()]

    List2 is a list of all elements Elem in List1 for which Pred(Elem) returns true.

flatmap(Fun, List1) -> List2

    Types
        Fun = fun(A) -> B
        List1 = [A]
        List2 = [B]
        A = B = term()

    flatmap behaves as if it had been defined as follows:
    flatmap(Fun, List1) ->
        append(map(Fun, List1))

foldl(Fun, Acc0, List) -> Acc1

    Types
        Fun = fun(Elem, AccIn) -> AccOut
        Elem = term()
        Acc0 = Acc1 = AccIn = AccOut = term()
        List = [term()]

    Calls Fun(Elem, AccIn) on successive elements A of List, starting with AccIn == Acc0. Fun/2 must return a new accumulator which is passed to the next call. The function returns the final value of the accumulator. Acc0 is returned if the list is empty. For example:
    >lists:foldl(fun(X, Sum) -> X + Sum end, 0, [1,2,3,4,5]).
    15
    >lists:foldl(fun(X, Prod) -> X * Prod end, 1, [1,2,3,4,5]).
    120

foldr(Fun, Acc0, List) -> Acc1

    Types
        Fun = fun(Elem, AccIn) -> AccOut
        Elem = term()
        Acc0 = Acc1 = AccIn = AccOut = term()
        List = [term()]

    Like foldl/3, but the list is traversed from right to left. For example:
    >P = fun(A, AccIn) -> io:format("~p ", [A]), AccIn end.
    #Fun<erl_eval.12.2225172>
    >lists:foldl(P, void, [1,2,3]).
    1 2 3 void
    >lists:foldr(P, void, [1,2,3]).
    3 2 1 void

    foldl/3 is tail recursive and would usually be preferred to foldr/3.

foreach(Fun, List) -> void()

    Types
        Fun = fun(Elem) -> void()
        Elem = term()
        List = [term()]

    Calls Fun(Elem) for each element Elem in List. This function is used for its side effects and the evaluation order is defined to be the same as the order of the elements in the list.

map(Fun, List1) -> List2

    Types
        Fun = fun(A) -> B
        List1 = [A]
        List2 = [B]
        A = B = term()

    Takes a function from As to Bs, and a list of As and produces a list of Bs by applying the function to every element in the list. This function is used to obtain the return values. The evaluation order is implementation dependent.

mapfoldl(Fun, Acc0, List1) -> {List2, Acc1}

    Types
        Fun = fun(A, AccIn) -> {B, AccOut}
        Acc0 = Acc1 = AccIn = AccOut = term()
        List1 = [A]
        List2 = [B]
        A = B = term()

    mapfold combines the operations of map/2 and foldl/3 into one pass. An example, summing the elements in a list and double them at the same time:
    >lists:mapfoldl(fun(X, Sum) -> {2*X, X+Sum} end,
                   0, [1,2,3,4,5]).
    {[2,4,6,8,10],15}

mapfoldr(Fun, Acc0, List1) -> {List2, Acc1}

    Types
        Fun = fun(A, AccIn) -> {B, AccOut}
        Acc0 = Acc1 = AccIn = AccOut = term()
        List1 = [A]
        List2 = [B]
        A = B = term()

    mapfold combines the operations of map/2 and foldr/3 into one pass.

partition(Pred, List) -> {Satisfying, NonSatisfying}

    Types
        Pred = fun(Elem) -> bool()
        Elem = term()
        List = Satisfying = NonSatisfying = [term()]

    Partitions List into two lists, where the first list contains all elements for which Pred(Elem) returns true, and the second list contains all elements for which Pred(Elem) returns false.

    Examples:
    >lists:partition(fun(A) -> A rem 2 == 1 end, [1,2,3,4,5,6,7]).
    {[1,3,5,7],[2,4,6]}
    >lists:partition(fun(A) -> is_atom(A) end, [a,b,1,c,d,2,3,4,e]).
    {[a,b,c,d,e],[1,2,3,4]}

    See also splitwith/2 for a different way to partition a list.

splitwith(Pred, List) -> {List1, List2}

    Types
        Pred = fun(Elem) -> bool()
        Elem = term()
        List = List1 = List2 = [term()]

    Partitions List into two lists according to Pred. splitwith/2 behaves as if it is defined as follows:
    splitwidth(Pred, List) ->
        {takewhile(Pred, List), dropwhile(Pred, List)}.

    Examples:
    >lists:splitwith(fun(A) -> A rem 2 == 1 end, [1,2,3,4,5,6,7]).
    {[1],[2,3,4,5,6,7]}
    >lists:splitwith(fun(A) -> is_atom(A) end, [a,b,1,c,d,2,3,4,e]).
    {[a,b],[1,c,d,2,3,4,e]}

    See also partition/2 for a different way to partition a list.

takewhile(Pred, List1) -> List2

    Types
        Pred = fun(Elem) -> bool()
        Elem = term()
        List1 = List2 = [term()]

    Takes elements Elem from List1 while Pred(Elem) returns true, that is, the function returns the longest prefix of the list for which all elements satisfy the predicate.

AUTHORS
Joe Armstrong - support@erlang.ericsson.se
Robert Virding - support@erlang.ericsson.se