lists(3)
๐ธ lists โ Erlang Module Definition ๐ธ
okay bestie, so this is lists(3) from the Erlang manual โ your one-stop shop for List processing functions. She's iconic, she's everywhere, and honestly every Erlang program you've ever loved is lowkey running on her. Let's get into it. ๐
๐ NAME ๐
lists โ List processing functions. That's it. That's the whole vibe. ๐
โจ DESCRIPTION โจ
This module contains functions for list processing. Periodt.
Unless otherwise stated, all functions assume that position numbering starts at 1. So the first element of a list lives at position 1, not 0, okay? We're not doing that 0-indexed nonsense here. ๐ซถ
Two terms T1 and T2 compare equal if T1 == T2 evaluates to true. They match if T1 =:= T2 evaluates to true. Yes, those are two different things and yes, it matters, iykyk. ๐
Whenever an ordering function F is expected as an argument, it's assumed that the following properties hold of F for all x, y, and z:
- If
x F yandy F x, thenx = y(Fis antisymmetric). - If
x F yandy F z, thenx F z(Fis transitive). F yory F x(Fis total).
An example of a typical ordering function is less than or equal to: =</2. Cute, right? ๐ธ
๐ฅ FUNCTIONS ๐ฅ
okay this is the main character section, buckle up bestie, there are SO many and they're all useful. ๐ฆ
all(Pred, List) -> boolean() โญ
all(Pred, List) -> boolean()
when
Pred :: fun((Elem :: T) -> boolean()),
List :: [T],
T :: term().
Returns true if Pred(Elem) returns true for all elements Elem in List; otherwise, returns false. It's the vibe check of list functions โ everybody has to pass or it's a no from her. ๐ซ
Examples
1> IsEven = fun(N) -> N rem 2 =:= 0 end.
2> lists:all(IsEven, [2,4,5]).
false
3> lists:all(IsEven, [2,4,6]).
true
any(Pred, List) -> boolean() โจ
any(Pred, List) -> boolean()
when
Pred :: fun((Elem :: T) -> boolean()),
List :: [T],
T :: term().
Returns true if Pred(Elem) returns true for at least one element Elem in List; otherwise, returns false. One single slay is enough to carry the whole list. ๐
Examples
1> IsEven = fun(N) -> N rem 2 =:= 0 end.
2> lists:any(IsEven, [3,5,7]).
false
3> lists:any(IsEven, [2,3,5,7]).
true
append(ListOfLists) -> List1 ๐ง
append(ListOfLists) -> List1
when
ListOfLists :: [List],
List :: [T],
List1 :: [T],
T :: term().
Returns a list in which all sublists of ListOfLists have been concatenated. All the little lists get merged into one big glorious list. ๐
Examples
1> lists:append([[1, 2, 3], [a, b], [4, 5, 6]]).
[1,2,3,a,b,4,5,6]
append(List1, List2) -> List3 ๐ซ
append(List1, List2) -> List3
when
List1 :: [T],
List2 :: [T],
List3 :: [T],
T :: term().
Returns a new list, List3, consisting of the elements of List1, followed by the elements of List2. Glued together, no drama, no notes. ๐ซถ
Examples
1> lists:append("abc", "def").
"abcdef"
lists:append(A, B) is equivalent to A ++ B. Same energy, two outfits. ๐ฏ
concat(Things) -> string() ๐
concat(Things) -> string()
when
Things :: [Thing],
Thing :: atom() | integer() | float() | string().
Concatenates the text representation of the elements of Things. It's giving "turn literally anything into a string and smash it together." โจ
The elements of Things can be atoms, integers, floats, or strings. Versatile queen.
Examples
1> lists:concat([doc, '/', file, '.', 3]).
"doc/file.3"
delete(Elem, List1) -> List2 ๐ฆ
delete(Elem, List1) -> List2
when Elem :: T, List1 :: [T], List2 :: [T], T :: term().
Returns a copy of List1 where the first element matching Elem is removed, if there is such an element. Only the first one bestie โ if there's a second, she's staying. ๐
Examples
1> lists:delete(b, [a,b,c]).
[a,c]
2> lists:delete(x, [a,b,c]).
[a,b,c]
droplast(List) -> InitList ๐
droplast(List) -> InitList
when List :: [T, ...], InitList :: [T], T :: term().
Drops the last element of a List. Bye bye final element, we barely knew ye. ๐
โ ๏ธ The list must be non-empty; otherwise, the function raises a function_clause exception. So like, don't. ๐ญ
Examples
1> lists:droplast([1]).
[]
2> lists:droplast([1,2,3]).
[1,2]
3> lists:droplast([]).
** exception error: no function clause matching lists:droplast([])
dropwhile(Pred, List1) -> List2 ๐ธ
dropwhile(Pred, List1) -> List2
when
Pred :: fun((Elem :: T) -> boolean()),
List1 :: [T],
List2 :: [T],
T :: term().
Drops elements Elem from List1 while Pred(Elem) returns true, and then returns the remaining list. Note the while โ she stops dropping the moment Pred says false, even if later elements would've passed. So real of her. โ
Examples
1> lists:dropwhile(fun is_atom/1, [a,b,c,1,2,3,x,y,z]).
[1,2,3,x,y,z]
2> lists:dropwhile(fun is_integer/1, [a,b,c,1,2,3,x,y,z]).
[a,b,c,1,2,3,x,y,z]
duplicate(N, Elem) -> List ๐
duplicate(N, Elem) -> List
when
N :: non_neg_integer(),
Elem :: T,
List :: [T],
T :: term().
Returns a list containing N copies of term Elem. Just, like, so many of the same thing. Main character behavior, honestly. ๐
Examples
1> lists:duplicate(5, xx).
[xx,xx,xx,xx,xx]
enumerate(Index, Step, List1) -> List2 ๐ข
enumerate(Index, Step, List1) -> List2
when
List1 :: [T],
List2 :: [{Index, T}],
Index :: integer(),
Step :: integer(),
T :: term().
Returns List1 with each element H replaced by a tuple of form {I, H}, where I is the position of H in List1. Glow-up: element โก๏ธ numbered tuple. โจ
The enumeration starts with Index and increases by Step in each step.
That is, enumerate/3 behaves as if it were defined as follows:
enumerate(I, S, List) ->
{List1, _ } = lists:mapfoldl(fun(T, Acc) -> {{Acc, T}, Acc+S} end, I, List),
List1.
The default values for Index and Step are both 1. So if you're lazy (valid), it just counts 1, 2, 3, 4โฆ ๐
Examples
1> lists:enumerate([a,b,c]).
[{1,a},{2,b},{3,c}]
2> lists:enumerate(10, [a,b,c]).
[{10,a},{11,b},{12,c}]
3> lists:enumerate(0, -2, [a,b,c]).
[{0,a},{-2,b},{-4,c}]
filter(Pred, List1) -> List2 ๐
filter(Pred, List1) -> List2
when
Pred :: fun((Elem :: T) -> boolean()),
List1 :: [T],
List2 :: [T],
T :: term().
Returns a list of elements Elem in List1 for which Pred(Elem) returns true. Everyone who passes Pred gets to stay. Everyone else? Blocked. ๐ซ๐
Examples
1> IsEven = fun(N) -> N rem 2 =:= 0 end.
2> lists:filter(IsEven, [1,2,3,4,5]).
[2,4]
filtermap(Fun, List1) -> List2 ๐ง
filtermap(Fun, List1) -> List2
when
Fun :: fun((Elem) -> boolean() | {true, Value}),
List1 :: [Elem],
List2 :: [Elem | Value],
Elem :: term(),
Value :: term().
Calls Fun(Elem) on successive elements Elem of List1 to update or remove elements from List1. She does two jobs at once and doesn't even break a sweat. ๐ฎโ๐จ
Fun/1 must return either a Boolean or a tuple {true, Value}. The function returns the list of elements for which Fun returns a new value, with true being equivalent to {true, Elem}.
That is, filtermap behaves as if it were defined as follows:
filtermap(Fun, List1) ->
lists:flatmap(fun(Elem) ->
case Fun(Elem) of
false -> [];
true -> [Elem];
{true,Value} -> [Value]
end
end, List1).
Examples
1> lists:filtermap(fun(X) ->
case X rem 2 of
0 -> {true, X div 2};
1 -> false
end
end, [1,2,3,4,5]).
[1,2]
flatlength(DeepList) -> non_neg_integer() ๐ฆ
flatlength(DeepList) -> non_neg_integer()
when DeepList :: [term() | DeepList].
Equivalent to length(flatten(DeepList)), but more efficient. Same answer, way less effort. Efficiency is the moment. โก
Examples
1> lists:flatlength([a,[b,c,[d,e]],f,[[g,h,i]]]).
9
2> lists:flatlength([[[]]]).
0
flatmap(Fun, List1) -> List2 โจ
flatmap(Fun, List1) -> List2
when
Fun :: fun((A) -> [B]),
List1 :: [A],
List2 :: [B],
A :: term(),
B :: term().
Takes a function from As to lists of Bs, and a list of As (List1), producing a list of Bs by applying the function to each element in List1 and appending the resulting lists. Map, then flatten, all in one glow-up. ๐
That is, flatmap behaves as if it were defined as follows:
flatmap(Fun, List1) ->
lists:append(lists:map(Fun, List1)).
Examples
1> lists:flatmap(fun(X)-> [X,X] end, [a,b,c]).
[a,a,b,b,c,c]
2> F = fun(N) when is_integer(N) -> [10 * N];
(_) -> []
end, ok.
3> lists:flatmap(F, [1,2,a,b,c,3]).
[10,20,30]
flatten(DeepList) -> List ๐
flatten(DeepList) -> List
when DeepList :: [term() | DeepList], List :: [term()].
Returns a flattened version of DeepList. All the nested nonsense? Gone. One clean list, one clean vibe. ๐ซง
Examples
1> lists:flatten([a,[b,c,[d,e]],f]).
[a,b,c,d,e,f]
flatten(DeepList, Tail) -> List ๐ธ
flatten(DeepList, Tail) -> List
when
DeepList :: [term() | DeepList],
Tail :: [term()],
List :: [term()].
Returns a flattened version of DeepList with tail Tail appended. Flatten and tack on the ending, two birds, one iconic function. ๐ฆ๐ฆ
Examples
1> lists:flatten([a,[b,c,[d,e]],f], [g,h,i]).
[a,b,c,d,e,f,g,h,i]
foldl(Fun, Acc0, List) -> Acc1 ๐
foldl(Fun, Acc0, List) -> Acc1
when
Fun :: fun((Elem :: T, AccIn) -> AccOut),
Acc0 :: term(),
Acc1 :: term(),
AccIn :: term(),
AccOut :: term(),
List :: [T],
T :: term().
Calls Fun(Elem, AccIn) on successive elements A of List, starting with AccIn bound to Acc0. Left to right, accumulating the whole way down. ๐งฌ
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. (Yes, Acc0 comes back untouched โ she's loyal like that. ๐)
Examples
1> lists:foldl(fun(X, Sum) -> X + Sum end, 0, [1,2,3,4,5]).
15
2> lists:foldl(fun(X, Prod) -> X * Prod end, 1, [1,2,3,4,5]).
120
foldr(Fun, Acc0, List) -> Acc1 ๐
foldr(Fun, Acc0, List) -> Acc1
when
Fun :: fun((Elem :: T, AccIn) -> AccOut),
Acc0 :: term(),
Acc1 :: term(),
AccIn :: term(),
AccOut :: term(),
List :: [T],
T :: term().
Like foldl/3, but the list is traversed from right to left. Same energy, opposite direction. She walks in backwards and still eats. ๐
okay bestie we are SO not done โ the lists module keeps going and so do we. part 2 incoming, don't go anywhere ๐ซถ
โจ foldl/3 & foldr/3 โ the examples โจ
1> P = fun(A, AccIn) -> [A|AccIn] end.
2> lists:foldl(P, [], [1,2,3]).
[3,2,1]
3> lists:foldr(P, [], [1,2,3]).
[1,2,3]
lowkey important: foldl/3 is tail-recursive and is usually preferred to foldr/3. ๐
๐ธ foreach(Fun, List) -> ok
foreach(Fun, List) -> ok
when
Fun :: fun((Elem :: T) -> term()),
List :: [T],
T :: term().
Calls Fun(Elem) for each element Elem in List, ignoring the return value. ๐ซถ
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. periodt.
๐ฆ join(Sep, List1) -> List2
join(Sep, List1) -> List2
when Sep :: T, List1 :: [T], List2 :: [T], T :: term().
Inserts Sep between each element in List1.
Has no effect on an empty list or a singleton list. it's giving nothing to work with, bestie. ๐
Examples:
1> lists:join(x, [a,b,c]).
[a,x,b,x,c]
2> lists:join(x, [a]).
[a]
3> lists:join(x, []).
[]
๐ keydelete(Key, N, TupleList1) -> TupleList2
keydelete(Key, N, TupleList1) -> TupleList2
when
Key :: term(),
N :: pos_integer(),
TupleList1 :: [Tuple],
TupleList2 :: [Tuple],
Tuple :: tuple().
Returns a copy of TupleList1, where the first occurrence of a tuple whose Nth element compares equal to Key is removed, if there is such a tuple. ๐ซถ
Examples:
1> lists:keydelete(c, 1, [{b,1}, {c,55}, {d,75}]).
[{b,1},{d,75}]
2> lists:keydelete(unknown, 1, [{b,1}, {c,55}, {d,75}]).
[{b,1},{c,55},{d,75}]
๐ keyfind(Key, N, TupleList) -> Tuple | false
keyfind(Key, N, TupleList) -> Tuple | false
when
Key :: term(),
N :: pos_integer(),
TupleList :: [Tuple],
Tuple :: tuple().
Searches the list of tuples TupleList for a tuple whose Nth element compares equal to Key.
Returns Tuple if such a tuple is found; otherwise, returns false. it's giving detective energy ๐
Examples:
1> lists:keyfind(b, 1, [{a,10}, {b,20}, {c,30}]).
{b,20}
2> lists:keyfind(unknown, 1, [{a,10}, {b,20}, {c,30}]).
false
โจ keymap(Fun, N, TupleList1) -> TupleList2
keymap(Fun, N, TupleList1) -> TupleList2
when
Fun :: fun((Term1 :: term()) -> Term2 :: term()),
N :: pos_integer(),
TupleList1 :: [Tuple],
TupleList2 :: [Tuple],
Tuple :: tuple().
Returns a list of tuples where, for each tuple in TupleList1, the Nth element Term1 of the tuple has been replaced with the result of calling Fun(Term1). glow up moment โจ
Examples:
1> Fun = fun(Atom) -> atom_to_list(Atom) end.
2> lists:keymap(Fun, 2, [{name,jane,22},{name,lizzie,20},{name,lydia,15}]).
[{name,"jane",22},{name,"lizzie",20},{name,"lydia",15}]
๐
keymember(Key, N, TupleList) -> boolean()
keymember(Key, N, TupleList) -> boolean()
when
Key :: term(),
N :: pos_integer(),
TupleList :: [Tuple],
Tuple :: tuple().
Returns true if TupleList contains a tuple whose Nth element compares equal to Key; otherwise, returns false. simple as that ๐
Examples:
1> lists:keymember(b, 1, [{a,10}, {b,20}, {c,30}]).
true
2> lists:keymember(unknown, 1, [{a,10}, {b,20}, {c,30}]).
false
๐ keymerge(N, TupleList1, TupleList2) -> TupleList3
keymerge(N, TupleList1, TupleList2) -> TupleList3
when
N :: pos_integer(),
TupleList1 :: [T1],
TupleList2 :: [T2],
TupleList3 :: [T1 | T2],
T1 :: Tuple,
T2 :: Tuple,
Tuple :: tuple().
Returns the sorted list formed by merging TupleList1 and TupleList2.
The merge is performed on the Nth element of each tuple. Both TupleList1 and TupleList2 must be key-sorted before evaluating this function. When the key elements of the two tuples compare equal, the tuple from TupleList1 is picked before the tuple from TupleList2. no cap. ๐ฅ
Examples:
1> lists:keymerge(2, [{b, 50}], [{c, 20}, {a, 50}]).
[{c,20},{b,50},{a,50}]
๐ keyreplace(Key, N, TupleList1, NewTuple) -> TupleList2
keyreplace(Key, N, TupleList1, NewTuple) -> TupleList2
when
Key :: term(),
N :: pos_integer(),
TupleList1 :: [Tuple],
TupleList2 :: [Tuple],
NewTuple :: Tuple,
Tuple :: tuple().
Returns a copy of TupleList1 where the first occurrence of a tuple T whose Nth element compares equal to Key is replaced with NewTuple, if there is such a tuple T. ๐ซถ
Examples:
1> lists:keyreplace(c, 1, [{b,1}, {c,55}, {d,75}], {new,tuple}).
[{b,1},{new,tuple},{d,75}]
2> lists:keyreplace(unknown, 1, [{b,1}, {c,55}, {d,75}], {new,tuple}).
[{b,1},{c,55},{d,75}]
๐ keysearch(Key, N, TupleList) -> {value, Tuple} | false
keysearch(Key, N, TupleList) -> {value, Tuple} | false
when
Key :: term(),
N :: pos_integer(),
TupleList :: [Tuple],
Tuple :: tuple().
Searches the list of tuples TupleList for a tuple whose Nth element compares equal to Key.
Returns {value, Tuple} if such a tuple is found; otherwise, returns false.
NOTE: This function is retained for backward compatibility. Function keyfind/3 is easier to use and more efficient. so maybe use that instead, bestie? ๐
๐ keysort(N, TupleList1) -> TupleList2
keysort(N, TupleList1) -> TupleList2
when
N :: pos_integer(),
TupleList1 :: [Tuple],
TupleList2 :: [Tuple],
Tuple :: tuple().
Returns a list of the elements in TupleList1, sorted by the Nth element of each tuple.
The sort is stable. periodt. โจ
Examples:
1> lists:keysort(2, [{a, 99}, {b, 17}, {c, 50}, {d, 50}]).
[{b,17},{c,50},{d,50},{a,99}]
๐ keystore(Key, N, TupleList1, NewTuple) -> TupleList2
keystore(Key, N, TupleList1, NewTuple) -> TupleList2
when
Key :: term(),
N :: pos_integer(),
TupleList1 :: [Tuple],
TupleList2 :: [Tuple, ...],
NewTuple :: Tuple,
Tuple :: tuple().
Returns a copy of TupleList1 with the first tuple whose Nth element compares equal to Key replaced by NewTuple, or with [NewTuple] appended if no such tuple exists. iconic ๐
Examples:
1> lists:keystore(b, 1, [{a, 10}, {b, 23}, {c, 99}], {bb, 1}).
[{a, 10}, {bb, 1}, {c, 99}]
2> lists:keystore(z, 1, [{a, 10}, {b, 23}, {c, 99}], {z, 2}).
[{a, 10}, {b, 23}, {c, 99}, {z, 2}]
๐
keytake(Key, N, TupleList1) -> {value, Tuple, TupleList2} | false
keytake(Key, N, TupleList1) -> {value, Tuple, TupleList2} | false
when
Key :: term(),
N :: pos_integer(),
TupleList1 :: [tuple()],
TupleList2 :: [tuple()],
Tuple :: tuple().
Searches the list of tuples TupleList1 for a tuple whose Nth element compares equal to Key, returning {value, Tuple, TupleList2} if found, where TupleList2 is a copy of TupleList1 with the first occurrence of Tuple removed.
Otherwise, returns false if no such tuple is found. it's giving take-and-leave energy ๐
Examples:
1> lists:keytake(b, 1, [{a, 10}, {b, 23}, {c, 99}]).
{value,{b,23},[{a, 10},{c, 99}]}
2> lists:keytake(z, 1, [{a, 10}, {b, 23}, {c, 99}]).
false
๐ญ last(List) -> Last
last(List) -> Last when List :: [T, ...], Last :: T, T :: term().
Returns the last element in List.
The list must be non-empty; otherwise, the function raises a function_clause exception. so don't try it with [], bestie. ๐ญ
Examples:
1> lists:last([1]).
1
2> lists:last([1,2,3]).
3
3> lists:last([]).
** exception error: no function clause matching lists:last([])
โจ map(Fun, List1) -> List2
map(Fun, List1) -> List2
when
Fun :: fun((A) -> B),
List1 :: [A],
List2 :: [B],
A :: term(),
B :: term().
Takes a function from As to Bs and a list of As, producing a list of Bs by applying the function to each element in the list. it's giving transformation โจ
Examples:
1> lists:map(fun(N) -> N + 1 end, [1,2,3]).
[2,3,4]
๐ฆ mapfoldl(Fun, Acc0, List1) -> {List2, Acc1}
mapfoldl(Fun, Acc0, List1) -> {List2, Acc1}
when
Fun :: fun((A, AccIn) -> {B, AccOut}),
Acc0 :: term(),
Acc1 :: term(),
AccIn :: term(),
AccOut :: term(),
List1 :: [A],
List2 :: [B],
A :: term(),
B :: term().
Combines the operations of map/2 and foldl/3 into one pass. two birds, one stone, no cap. ๐ฆ
Examples:
Summing the elements in a list and double them at the same time:
1> 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}
mapfoldr(Fun, Acc0, List1) -> {List2, Acc1}
when
Fun :: fun((A, AccIn) -> {B, AccOut}),
Acc0 :: term(),
Acc1 :: term(),
AccIn :: term(),
AccOut :: term(),
List1 :: [A],
List2 :: [B],
A :: term(),
B :: term().
Combines the operations of map/2 and foldr/3 into one pass.
NOTE: Unless the order in which the elements are accumulated is important, prefer mapfoldl/3 as it is slightly more efficient. periodt. ๐
Examples:
Doubling the elements in list and producing a list of squares at the same time:
1> lists:mapfoldr(fun(X, Acc) -> {2*X, [X*X|Acc]} end, [], [1,2,3,4,5]).
{[2,4,6,8,10],[1,4,9,16,25]}
๐ max(List) -> Max
max(List) -> Max when List :: [T, ...], Max :: T, T :: term().
Returns the first element of List that compares greater than or equal to all other elements of List. it's giving main character energy ๐
Examples:
1> lists:max([17,19,7,55]).
55
2> lists:max([]).
** exception error: no function clause matching lists:max([])
๐ member(Elem, List) -> boolean()
member(Elem, List) -> boolean()
when Elem :: T, List :: [T], T :: term().
Okay so member/2 is wrapping up with this vibe: returns true if Elem matches some element of List; otherwise, it returns false. Periodt. ๐
1> lists:member(2, [1,2,3]).
true
2> lists:member(nope, [1,2,3]).
false
merge(ListOfLists) -> List1
merge(ListOfLists) -> List1
when
ListOfLists :: [List],
List :: [T],
List1 :: [T],
T :: term().
Returns the sorted list formed by merging all sublists of ListOfLists. It's giving merge energy. โจ
All sublists must be sorted before evaluating this function. No cap, bestie, sort them first or it's a whole mess. ๐ญ
When two elements compare equal, the element from the sublist with the lowest position in ListOfLists is picked before the other element. That's the rule. ๐
Examples:
1> lists:merge([[b,l,l], [g,k,q]]).
[b,g,k,l,l,q]
merge(List1, List2) -> List3
merge(List1, List2) -> List3
when
List1 :: [X],
List2 :: [Y],
List3 :: [X | Y],
X :: term(),
Y :: term().
Returns the sorted list formed by merging List1 and List2. ๐ธ
Both List1 and List2 must be sorted before evaluating this function. Like, fr fr, don't skip that step. ๐ซถ
When two elements compare equal, the element from List1 is picked before the element from List2. Main character energy from List1, I guess. ๐
Examples:
1> lists:merge([a,o], [b,x]).
[a,b,o,x]
merge(Fun, List1, List2) -> List3
merge(Fun, List1, List2) -> List3
when
Fun :: fun((A, B) -> boolean()),
List1 :: [A],
List2 :: [B],
List3 :: [A | B],
A :: term(),
B :: term().
Returns a sorted list formed by merging List1 and List2 based on Fun. Custom ordering? We love to see it. ๐
Both List1 and List2 must be sorted according to the ordering function Fun before evaluating this function. So Fun is the vibe check. โจ
Fun(A, B) is to return true if A compares less than or equal to B in the ordering, otherwise false. When two elements compare equal, the element from List1 is picked before the element from List2. ๐
Examples:
1> F = fun(A, B) -> tuple_size(A) =< tuple_size(B) end.
2> lists:merge(F, [{x, y}, {a, b, c}], [{q, w}]).
[{x,y},{q,w},{a,b,c}]
merge3(List1, List2, List3) -> List4
merge3(List1, List2, List3) -> List4
when
List1 :: [X],
List2 :: [Y],
List3 :: [Z],
List4 :: [X | Y | Z],
X :: term(),
Y :: term(),
Z :: term().
Returns the sorted list formed by merging List1, List2, and List3. Three lists? Iconic. ๐
All of List1, List2, and List3 must be sorted before evaluating this function. Yes, all three. Don't play. ๐ญ
When two elements compare equal, the element from List1, if there is such an element, is picked before the other element, otherwise the element from List2 is picked before the element from List3. The hierarchy is real. ๐
Examples:
1> lists:merge3([a,o], [g,q], [j]).
[a,g,j,o,q]
min(List) -> Min
min(List) -> Min when List :: [T, ...], Min :: T, T :: term().
Returns the first element of List that compares less than or equal to all other elements of List. It's the smallest, but if there are ties, the first one wins. ๐ฅ
Examples:
1> lists:min([17,19,7,55]).
7
2> lists:min([]).
** exception error: no function clause matching lists:min([])
Not me passing an empty list and getting an exception. ๐
nth(N, List) -> Elem
nth(N, List) -> Elem
when
N :: pos_integer(),
List :: [T, ...],
Elem :: T,
T :: term().
Returns the Nth element of List. Counting starts at 1, bestie. โจ
Examples:
1> lists:nth(3, [a, b, c, d, e]).
c
nthtail(N, List) -> Tail
nthtail(N, List) -> Tail
when
N :: non_neg_integer(),
List :: [T],
Tail :: [T],
T :: term().
Returns the Nth tail of List, meaning the sublist of List starting at N+1 and continuing to the end of the list. So it drops the first N elements. ๐ฆ
Examples:
1> lists:nthtail(3, [a, b, c, d, e]).
[d,e]
2> tl(tl(tl([a, b, c, d, e]))).
[d,e]
3> lists:nthtail(0, [a, b, c, d, e]).
[a,b,c,d,e]
4> lists:nthtail(5, [a, b, c, d, e]).
[]
partition(Pred, List) -> {Satisfying, NotSatisfying}
partition(Pred, List) -> {Satisfying, NotSatisfying}
when
Pred :: fun((Elem :: T) -> boolean()),
List :: [T],
Satisfying :: [T],
NotSatisfying :: [T],
T :: term().
Partitions List into two lists: the first containing elements for which Pred(Elem) returns true, and the second containing elements for which Pred(Elem) returns false. It's the yes/no split. ๐
Examples:
1> lists:partition(fun(A) -> A rem 2 =:= 1 end, [1,2,3,4,5,6,7]).
{[1,3,5,7],[2,4,6]}
2> 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]}
For a different way to partition a list, see splitwith/2. ๐
prefix(List1, List2) -> boolean()
prefix(List1, List2) -> boolean()
when List1 :: [T], List2 :: [T], T :: term().
Returns true if List1 is a prefix of List2; otherwise, returns false. ๐
A prefix of a list is the first part of the list, starting from the beginning and stopping at any point. So it's like, does List2 start with List1? Iykyk. ๐
Examples:
1> lists:prefix("abc", "abcdef").
true
2> lists:prefix("def", "abcdef").
false
3> lists:prefix([], "any list").
true
4> lists:prefix("abc", "abc").
true
reverse(List1) -> List2
reverse(List1) -> List2
when List1 :: [T], List2 :: [T], T :: term().
Returns a list containing the elements in List1 in reverse order. Backwards slay. ๐
Examples:
1> lists:reverse([1,2,3]).
[3,2,1]
reverse(List1, Tail) -> List2
reverse(List1, Tail) -> List2
when
List1 :: [T],
Tail :: term(),
List2 :: [T],
T :: term().
Returns a list containing the elements of List1 in reverse order, with tail Tail appended. So it's like reverse plus a little extra at the end. ๐
Examples:
1> lists:reverse([1, 2, 3, 4], [a, b, c]).
[4,3,2,1,a,b,c]
search(Pred, List) -> {value, Value} | false
search(Pred, List) -> {value, Value} | false
when
Pred :: fun((T) -> boolean()),
List :: [T],
Value :: T.
If there is a Value in List such that Pred(Value) returns true, returns {value, Value} for the first such Value; otherwise, returns false. It's a scavenger hunt for the first match. ๐
Examples:
1> lists:search(fun is_atom/1, [1,2,3,a,b,c]).
{value,a}
2> lists:search(fun(#{a := V}) -> V =:= 42 end,
[#{a => 1}, #{a => 42}, #{a => 100}]).
{value,#{a => 42}}
seq(From, To, Incr) -> Seq
seq(From, To, Incr) -> Seq
when
From :: integer(),
To :: integer(),
Incr :: integer(),
Seq :: [integer()].
Returns a sequence of integers that starts with From and contains the successive results of adding Incr to the previous element, until To is reached or passed (in the latter case, To is not an element of the sequence). ๐
Incr defaults to 1. So if you don't pass it, it's just counting by ones. โจ
Failures
- If
To < From - IncrandIncr > 0. - If
To > From - IncrandIncr < 0. - If
Incr =:= 0andFrom =/= To.
The following equalities hold for all sequences:
length(lists:seq(From, To)) =:= To - From + 1
length(lists:seq(From, To, Incr)) =:= (To - From + Incr) div Incr
Examples:
1> lists:seq(1, 10).
[1,2,3,4,5,6,7,8,9,10]
2> lists:seq(1, 20, 3).
[1,4,7,10,13,16,19]
3> lists:seq(1, 0, 1).
[]
4> lists:seq(10, 6, 4).
[]
5> lists:seq(1, 1, 0).
[1]
sort(List1) -> List2
sort(List1) -> List2 when List1 :: [T], List2 :: [T], T :: term().
Returns a list containing the sorted elements of List1. ๐ฅ
The sort is stable. That means equal elements keep their original order. No chaos. ๐
Examples:
1> lists:sort([4,1,3,2]).
[1,2,3,4]
2> lists:sort([a,4,3,b,9]).
[3,4,9,a,b]
Since the sort is stable, the relative order of elements that compare equal is not changed:
1> lists:sort([1.0,1]).
[1.0,1]
2> lists:sort([1,1.0]).
[1,1.0]
sort(Fun, List1) -> List2
sort(Fun, List1) -> List2
when
Fun :: fun((A :: T, B :: T) -> boolean()),
List1 :: [T],
List2 :: [T],
T :: term().
Returns a list of the elements in List1, sorted according to the ordering function Fun, where Fun(A, B) returns true if A compares less than or equal to B in the ordering; otherwise, it returns false. Custom sort? We love. ๐
Examples:
1> F = fun(A, B) -> tuple_size(A) =< tuple_size(B) end.
2> lists:sort(F, [{a, b, c}, {x, y}, {q, w}]).
[{x,y},{q,w},{a,b,c}]
split(N, List1) -> {List2, List3}
split(N, List1) -> {List2, List3}
when
N :: non_neg_integer(),
List1 :: [T],
List2 :: [T],
List3 :: [T],
T :: term().
Splits List1 into List2, containing the first N elements, and List3, containing the rest. โ๏ธ
Examples:
1> lists:split(3, [1,2,3,4,5,6,7]).
{[1,2,3],[4,5,6,7]}
splitwith(Pred, List) -> {List1, List2}
splitwith(Pred, List) -> {List1, List2}
when
Pred :: fun((T) -> boolean()),
List :: [T],
List1 :: [T],
List2 :: [T],
T :: term().
Partitions List into two lists according to Pred. ๐ฆ
splitwith/2 behaves as if it were defined as follows:
splitwith(Pred, List) ->
{takewhile(Pred, List), dropwhile(Pred, List)}.
Examples:
1> lists:splitwith(fun(A) -> A rem 2 =:= 1 end, [1,2,3,4,5,6,7]).
{[1],[2,3,4,5,6,7]}
2> 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]}
For a different way to partition a list, see partition/2. ๐
sublist(List1, Len) -> List2
sublist(List1, Len) -> List2
when
List1 :: [T],
List2 :: [T],
Len :: non_neg_integer(),
T :: term().
Returns the sublist of List1 starting at position 1 and with no more than Len elements. So it's the first Len elements, basically. โจ
okay so bestie, real talk: it is not an error for Len to exceed the length of the list. In that case, the whole list is returned. No cap. ๐
Examples โจ
1> lists:sublist([1,2,3,4,5], 2).
[1,2]
2> lists:sublist([1,2,3,4,5], 99).
[1,2,3,4,5]
sublist(List1, Start, Len) -> List2
sublist(List1, Start, Len) -> List2
when
List1 :: [T],
List2 :: [T],
Start :: pos_integer(),
Len :: non_neg_integer(),
T :: term().
Returns the sublist of List1 starting at Start and with no more than Len elements.
It is not an error for Start+Len to exceed the length of the list. So like, if you ask for more than there is, you just get what's there. Slay.
Examples ๐ธ
1> lists:sublist([1,2,3,4], 2, 2).
[2,3]
2> lists:sublist([1,2,3,4], 2, 5).
[2,3,4]
3> lists:sublist([1,2,3,4], 5, 2).
[]
subtract(List1, List2) -> List3
subtract(List1, List2) -> List3
when
List1 :: [T],
List2 :: [T],
List3 :: [T],
T :: term().
Returns a new list, List3, which is a copy of List1 with the following modification: for each element in List2, its first occurrence in List1 is removed. It's giving "subtract but only the first time you see it". ๐
Examples ๐
1> lists:subtract("123212", "212").
"312"
lists:subtract(A, B) is equivalent to A -- B. iykyk.
suffix(List1, List2) -> boolean()
suffix(List1, List2) -> boolean()
when List1 :: [T], List2 :: [T], T :: term().
Returns true if List1 is a suffix of List2; otherwise, returns false.
A suffix of a list is the last part of the list, starting from any position and going all the way to the end. So like the tail end vibe check. ๐ฆ
Examples โจ
1> lists:suffix("abc", "abcdef").
false
2> lists:suffix("def", "abcdef").
true
3> lists:suffix([], "any list").
true
4> lists:suffix("abc", "abc").
true
sum(List) -> number()
sum(List) -> number() when List :: [number()].
Returns the sum of the elements in List. Math girlies rise. โ
Examples ๐ฅ
1> lists:sum([]).
0
2> lists:sum([1,2,3]).
6
takewhile(Pred, List1) -> List2
takewhile(Pred, List1) -> List2
when
Pred :: fun((Elem :: T) -> boolean()),
List1 :: [T],
List2 :: [T],
T :: term().
Takes elements Elem from List1 while Pred(Elem) returns true, returning the longest prefix in which all elements satisfy the predicate. So it's like "keep going until the vibe flips". ๐ง
Examples ๐
1> lists:takewhile(fun is_atom/1, [a,b,c,1,2,3,x,y,z]).
[a,b,c]
2> lists:takewhile(fun is_integer/1, [a,b,c,1,2,3,x,y,z]).
[]
ukeymerge(N, TupleList1, TupleList2) -> TupleList3
ukeymerge(N, TupleList1, TupleList2) -> TupleList3
when
N :: pos_integer(),
TupleList1 :: [T1],
TupleList2 :: [T2],
TupleList3 :: [T1 | T2],
T1 :: Tuple,
T2 :: Tuple,
Tuple :: tuple().
Returns the sorted list formed by merging TupleList1 and TupleList2 based on the Nth element of each tuple.
Both TupleList1 and TupleList2 must be key-sorted without duplicates before evaluating this function. No cap.
When the Nth elements of two tuples compare equal, the tuple from TupleList1 is picked and the one from TupleList2 is removed. First come, first served, bestie. ๐
Examples ๐
1> lists:ukeymerge(1, [{a, 33}, {c, 15}], [{a, 59}, {d, 39}]).
[{a,33},{c,15},{d,39}]
ukeysort(N, TupleList1) -> TupleList2
ukeysort(N, TupleList1) -> TupleList2
when
N :: pos_integer(),
TupleList1 :: [Tuple],
TupleList2 :: [Tuple],
Tuple :: tuple().
Returns a sorted list of the elements in TupleList1, keeping only the first occurrence of tuples whose Nth elements compare equal.
Sorting is performed on the Nth element of the tuples. So like, sort by that one specific slot. ๐
Examples โญ
1> lists:ukeysort(2, [{a, 27}, {d, 23}, {e, 23}]).
[{d,23}, {a, 27}]
umerge(ListOfLists) -> List1
umerge(ListOfLists) -> List1
when
ListOfLists :: [List],
List :: [T],
List1 :: [T],
T :: term().
Returns a sorted list formed by merging all sublists in ListOfLists, while removing duplicates.
All sublists must be sorted and contain no duplicates before evaluating this function. So like, pre-sorted and pre-cleaned, periodt.
When two elements compare equal, the element from the sublist with the lowest position in ListOfLists is picked and the other is removed. The earliest sublist wins. ๐
Examples ๐
1> lists:umerge([[a,b], [a,d,e]]).
[a,b,d,e]
umerge(List1, List2) -> List3
umerge(List1, List2) -> List3
when
List1 :: [X],
List2 :: [Y],
List3 :: [X | Y],
X :: term(),
Y :: 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 before evaluating this function.
When two elements compare equal, the element from List1 is picked and the one from List2 is removed. List1 gets priority, bestie. ๐
Examples โจ
1> lists:umerge([a,b], [a,d,e]).
[a,b,d,e]
umerge(Fun, List1, List2) -> List3
umerge(Fun, List1, List2) -> List3
when
Fun :: fun((A, B) -> boolean()),
List1 :: [A],
List2 :: [B],
List3 :: [A | B],
A :: term(),
B :: term().
Returns a sorted list by merging List1 and List2 using ordering function Fun, assuming both lists are pre-sorted according to Fun and contain no duplicates.
Fun(A, B) is to return true if A compares less than or equal to B in the ordering; otherwise, it should return false. When two elements compare equal, the element from List1 is picked and the one from List2 is removed.
Examples ๐ธ
1> F = fun(A, B) -> tuple_size(A) =< tuple_size(B) end.
2> lists:umerge(F, [{x, y}, {a, b, c}], [{q, w}, {x, y, z, w}]).
[{x,y},{a,b,c},{x,y,z,w}]
umerge3(List1, List2, List3) -> List4
umerge3(List1, List2, List3) -> List4
when
List1 :: [X],
List2 :: [Y],
List3 :: [Z],
List4 :: [X | Y | Z],
X :: term(),
Y :: term(),
Z :: 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 before evaluating this function.
When two elements compare equal, the element from List1 is picked if there is such an element, otherwise the element from List2 is picked, and the other is removed. So like a priority queue: List1 > List2 > List3. ๐
Examples ๐ฅ
1> lists:umerge3([a,b], [a,d,e], [b,f]).
[a,b,d,e,f]
uniq(List1) -> List2
uniq(List1) -> List2 when List1 :: [T], List2 :: [T], T :: term().
Returns a list containing the elements of List1 with duplicated elements removed (preserving the order of the elements).
The first occurrence of each element is kept. So it's like "I've seen this one, next". ๐
Examples ๐ฆ
1> lists:uniq([3, 3, 1, 2, 1, 2, 3]).
[3,1,2]
2> lists:uniq([a, a, 1, b, 2, a, 3]).
[a, 1, b, 2, 3]
uniq(Fun, List1) -> List2
uniq(Fun, List1) -> List2
when
Fun :: fun((T) -> any()),
List1 :: [T],
List2 :: [T],
T :: term().
Returns a list containing the elements of List1 without the elements for which Fun returned duplicate values (preserving the order of the elements).
The first occurrence of each element is kept. So Fun decides what "duplicate" means. Main character energy. โจ
Examples ๐
1> lists:uniq(fun({X, _}) -> X end, [{b, 2}, {a, 1}, {c, 3}, {a, 2}]).
[{b, 2}, {a, 1}, {c, 3}]
unzip(List1) -> {List2, List3}
unzip(List1) -> {List2, List3}
when
List1 :: [{A, B}],
List2 :: [A],
List3 :: [B],
A :: term(),
B :: 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. It's giving "split the couple, bestie". ๐ซถ
Examples ๐
1> lists:unzip([{1, a}, {2, b}]).
{[1,2],[a,b]}
unzip3(List1) -> {List2, List3, List4}
unzip3(List1) -> {List2, List3, List4}
when
List1 :: [{A, B, C}],
List2 :: [A],
List3 :: [B],
List4 :: [C],
A :: term(),
B :: term(),
C :: 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. Three-way split, iconic. ๐
Examples โญ
1> lists:unzip3([{a, 1, 2}, {b, 777, 999}]).
{[a,b],[1,777],[2,999]}
usort(List1) -> List2
usort(List1) -> List2 when List1 :: [T], List2 :: [T], T :: term().
Returns a sorted list of the elements of List1, keeping only the first occurrence of elements that compare equal. So it's sort + dedupe, no cap. ๐ฅ
Examples ๐
1> lists:usort([a,x,y,b,c,x,a]).
[a,b,c,x,y]
2> lists:usort([3,2,a,3,2,a,1,3,b,2,2,1]).
[1,2,3,a,b]
3> lists:usort([1.0,1]).
[1.0]
4> lists:usort([1,1.0]).
[1]
usort(Fun, List1) -> List2
usort(Fun, List1) -> List2
when
Fun :: fun((T, T) -> boolean()),
List1 :: [T],
List2 :: [T],
T :: term().
Returns a list containing the sorted elements of List1 where all except the first element of the elements comparing equal according to the ordering function Fun have been removed.
Fun(A, B) is to return true if A compares less than or equal to B in the ordering, otherwise false.
Examples ๐
1> F = fun(A, B) -> tuple_size(A) =< tuple_size(B) end.
2> lists:usort(F, [{a, b, c}, {x, y}, {q, w}]).
[{x,y},{a,b,c}]
zip/3 โ the two-list zipper that ate ๐
zip(List1, List2, How) -> List3
when
List1 :: [A],
List2 :: [B],
List3 :: [{A | DefaultA, B | DefaultB}],
A :: term(),
B :: term(),
How :: fail | trim | {pad, {DefaultA, DefaultB}},
DefaultA :: term(),
DefaultB :: term().
Okay so picture this: you have two lists and you want them to become one list of two-tuples. The first element of each tuple comes from List1, and the second element comes from the corresponding element in List2. That's the whole zip fantasy, no cap.
The How parameter is literally the "what if the lists aren't the same length?" knob. It can be fail, trim, or {pad, {DefaultA, DefaultB}}.
fail โ no mismatches, periodt ๐ซ
The call will fail if the given lists are not of equal length. This is the default. So if you don't pass How, bestie, you better make those lengths match.
trim โ bye, surplus โ๏ธ
Surplus elements from the longer list will be ignored. That's it, that's the tweet.
Examples:
1> lists:zip([a, b], [1, 2, 3], trim).
[{a,1},{b,2}]
2> lists:zip([a, b, c], [1, 2], trim).
[{a,1},{b,2}]
{pad, Defaults} โ fill the gap ๐ซถ
The shorter list will be padded to the length of the longer list, using the respective elements from the given Defaults tuple.
Examples:
1> lists:zip([a, b], [1, 2, 3], {pad, {x, 0}}).
[{a,1},{b,2},{x,3}]
2> lists:zip([a, b, c], [1, 2], {pad, {x, 0}}).
[{a,1},{b,2},{c,0}]
zip3/4 โ three lists, three-tuple realness ๐ฅ
zip3(List1, List2, List3, How) -> List4
when
List1 :: [A],
List2 :: [B],
List3 :: [C],
List4 :: [{A | DefaultA, B | DefaultB, C | DefaultC}],
A :: term(),
B :: term(),
C :: term(),
How ::
fail | trim |
{pad, {DefaultA, DefaultB, DefaultC}},
DefaultA :: term(),
DefaultB :: term(),
DefaultC :: term().
"Zips" three lists into one list of three-tuples. The first element of each tuple is taken from the first list, the second element is taken from the corresponding element in the second list, and the third element is taken from the corresponding element in the third list.
For a description of the How parameter, see zip/3.
Examples:
1> lists:zip3([a], [1, 2, 3], [17, 19], trim).
[{a,1,17}]
2> lists:zip3([a], [1, 2, 3], [17, 19], {pad, {z, 0, 0}}).
[{a,1,17}, {z,2,19}, {z,3,0}]
zipwith/4 โ combine era ๐
zipwith(Combine, List1, List2, How) -> List3
when
Combine :: fun((X | DefaultX, Y | DefaultY) -> T),
List1 :: [X],
List2 :: [Y],
List3 :: [T],
X :: term(),
Y :: term(),
How :: fail | trim | {pad, {DefaultX, DefaultY}},
DefaultX :: term(),
DefaultY :: term(),
T :: term().
Combines the elements of two lists into a single list using the Combine fun.
For each pair X, Y of list elements from the two lists, the element in the result list is Combine(X, Y).
For a description of the How parameter, see zip/3.
zipwith(fun(X, Y) -> {X,Y} end, List1, List2) is equivalent to zip(List1, List2).
Examples:
1> lists:zipwith(fun(X, Y) -> X+Y end, [1,2,3], [4,5,6], fail).
[5,7,9]
zipwith3/5 โ triple threat ๐
zipwith3(Combine, List1, List2, List3, How) -> List4
when
Combine ::
fun((X | DefaultX, Y | DefaultY, Z | DefaultZ)
->
T),
List1 :: [X],
List2 :: [Y],
List3 :: [Z],
List4 :: [T],
X :: term(),
Y :: term(),
Z :: term(),
How ::
fail | trim |
{pad, {DefaultX, DefaultY, DefaultZ}},
DefaultX :: term(),
DefaultY :: term(),
DefaultZ :: term(),
T :: term().
Combines the elements of three lists into a single list using the Combine fun.
For each triple X, Y, Z of list elements from the three lists, the element in the result list is Combine(X, Y, Z).
For a description of the How parameter, see zip/3.
zipwith3(fun(X, Y, Z) -> {X,Y,Z} end, List1, List2, List3) is equivalent to zip3(List1, List2, List3).
Examples:
1> lists:zipwith3(fun(X, Y, Z) -> X+Y+Z end, [1,2,3], [4,5,6], [7,8,9], fail).
[12,15,18]
2> lists:zipwith3(fun(X, Y, Z) -> [X,Y,Z] end, [a,b,c], [x,y,z], [1,2,3], fail).
[[a,x,1],[b,y,2],[c,z,3]]
That's the whole page, bestie. Source is Ericsson AB, version is stdlib 8.0.4, and the page is lists(3). There's no date on this footer, so I'm not inventing one. Stay zipped ๐
๐ฆ where this page comes from
- ubuntu stonking: erlang-doc 1:29.0.6+dfsg-1 ยท Apache-2.0
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms