Lists & the lists Module

Efficiency notes:

[H | T] prepend is O(1); ++ is O(length of left list).

length/1 is O(n).

Build lists in reverse then lists:reverse/1 at the end.

lists:seq(1, 10),                     % [1..10]
lists:seq(1, 10, 3),                  % [1,4,7,10]
lists:reverse([1, 2, 3]),
lists:nth(2, [a, b, c]),              % b (1-based)
lists:nthtail(1, [a, b, c]),          % [b,c]
lists:last([1, 2, 3]),
lists:append([[1], [2, 3]]),          % [1,2,3]
lists:flatten([1, [2, [3]]]),
lists:member(2, [1, 2, 3]),
lists:sort([3, 1, 2]),
lists:sort(fun(A, B) -> A >= B end, [3, 1, 2]),   % descending
lists:usort([3, 1, 3]),               % sort + unique
lists:sum([1, 2, 3]), lists:max([1, 5]), lists:min([1, 5]),
lists:zip([1, 2], [a, b]),            % [{1,a},{2,b}]
lists:unzip([{1, a}, {2, b}]),
lists:zipwith(fun(A, B) -> A + B end, [1, 2], [3, 4]),
lists:split(2, [1, 2, 3, 4]),         % {[1,2],[3,4]}
lists:splitwith(fun(X) -> X < 3 end, [1, 2, 3, 1]),
lists:partition(fun(X) -> X > 2 end, [1, 2, 3, 4]),   % {[3,4],[1,2]}
lists:takewhile(fun(X) -> X < 3 end, [1, 2, 3, 1]),
lists:dropwhile(fun(X) -> X < 3 end, [1, 2, 3, 1]),
lists:delete(b, [a, b, c, b]),        % [a,c,b]
lists:subtract([1, 2, 3], [2]),
lists:duplicate(3, x),                % [x,x,x]
lists:sublist([1, 2, 3, 4], 2, 2),    % [2,3]
lists:prefix([1], [1, 2]), lists:suffix([2], [1, 2]),

…