August 23, 2024

P20 - Remove the Kth element from a list.

Return the list and the removed element in a Tuple. Elements are numbered from 0.

Example:

> p20:removeAt(3, [1,2,3,4,5,6]).
    {[1,2,3,5,6],4}

erlang

%Remove the Kth element from a list.

-module(p20).
-export([removeAt/2]).

removeAt(N, Ls) ->
    [A, B] = p17:split(N + 1, Ls),
    [Front, [Elem | []]] = p17:split(length(A) - 1, A),
    {Front ++ B, Elem}.
Be first to comment
Leave a reply