erlang % Imperative Solution
CatList = categorise(List),
-module(categorise).
-export([start/0]).
start() ->
List = [one, two, three, four, five],
% Imperative Solution
CatList = categorise(List),
io:format("~w~n", [CatList]).
categorise(List) ->
AccTbl = ets:new(the_map_name, [ordered_set, private, {keypos, 1}]),
Updater = fun (X) -> Key = length(atom_to_list(X)), ets:update_element(AccTbl, Key, {2, lists:reverse([X|ets:lookup_element(AccTbl, Key, 2)])}) end,
ets:insert(AccTbl, lists:map(fun (X) -> {length(atom_to_list(X)), []} end, List)),
lists:foreach(Updater, List),
CatList = ets:tab2list(AccTbl),
ets:delete(AccTbl),
CatList.