August 23, 2024

P21 - Insert an element at a given position into a list.

Example:

> p21:insertAt(3, 2, [1,2,4,5,6]).
    [1,2,3,4,5,6]

erlang

%Insert an element at a given position into a list.
-module(p21).
-export([insertAt/3]).

insertAt(Elem, Index, Ls) -> 
    [A, B] = p17:split(Index, Ls),
    A ++ [Elem] ++ B.
Be first to comment
Leave a reply