Based on Ninety-Nine Prolog Problems and Ninety-Nine Lisp Problems.
Problem 21
Insert an element at a given position into a list.
Example:
* (insert-at 'alfa '(a b c d) 2) (A ALFA B C D)
Solution:
-module(insert).
-export([insert/3]).
insert(List, 1, Value) ->
[Value | List];
insert(List, Pos, Value) ->
exchange(insert(List, 1, Value), Pos).
exchange(List, 1) ->
List;
exchange([H1 | [H2 | T]], Pos) ->
[H2 | exchange([H1 | T], Pos - 1)].