Skip to content

Instantly share code, notes, and snippets.

@yrashk
Created November 13, 2009 07:49
Show Gist options
  • Save yrashk/233663 to your computer and use it in GitHub Desktop.
Save yrashk/233663 to your computer and use it in GitHub Desktop.
insert(Key, Value, {trie, L}) when is_list(Key), is_list(L) ->
insert_1(Key, Value, L,trie).
insert_1([H|T], Value, [], Val) ->
{Val, [{H, insert_1(T, Value, [], undefined)}]};
insert_1(_Key, Value, [], _) ->
{Value, []};
insert_1([H|T], Value, L, Val) when is_list(L) ->
case lists:keymember(H,1,L) of
false ->
insert_1([H|T], Value, [{H,{undefined, []}} | L], Val);
true ->
{Val, insert_1([H|T], Value, {list, L}, Val)}
end;
insert_1([], Value, L, _) when is_list(L) ->
{Value, L};
insert_1([H|T], Value, {list, [LH|LT]}, Val) ->
[insert_1([H|T], Value, {item, LH}, Val) | insert_1([H|T], Value, {list, LT}, Val)];
insert_1([_H|_T], _Value, {list, []}, _Val) ->
[];
insert_1([H|T], Value, {item, {H, {NextVal, Kont}}}, _Val) ->
{H, insert_1(T, Value, Kont, NextVal)};
insert_1([_H|_T], _Value, {item, V}, _Val) ->
V.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment