Erlang Bubble sort - erlang

I'm learning Erlang and decided to implement bubble sort in it, it took me some effort and in result i've succeded, but i see that my way of thinking is incorrect, is there more effecient way to implement it or not?
bubble_sort(L) ->
if
length(L) > 1 ->
SL=bubble_sort_p(L),
bubble_sort(lists:sublist(SL,1,length(SL)-1)) ++ [lists:last(SL)];
true -> L
end.
bubble_sort_p([]) -> [];
bubble_sort_p([F | R]) ->
case length(R) > 0 of
true -> case F > hd(R) of
true -> [hd(R)] ++ bubble_sort_p([F|tl(R)]);
false -> [F] ++ bubble_sort_p([hd(R)|tl(R)])
end;
false -> [F]
end.

The implementation from the top of my head would be:
bubble_sort(L) -> bubble_sort(L, [], false).
bubble_sort([A, B | T], Acc, _) when A > B ->
bubble_sort([A | T], [B | Acc], true);
bubble_sort([A, B | T], Acc, Tainted) ->
bubble_sort([B | T], [A | Acc], Tainted);
bubble_sort([A | T], Acc, Tainted) ->
bubble_sort(T, [A | Acc], Tainted);
bubble_sort([], Acc, true) ->
bubble_sort(lists:reverse(Acc));
bubble_sort([], Acc, false) ->
lists:reverse(Acc).
If we are talking efficiency, we obvioulsy should not go for bubble sort in the first place.

Allow me to re-write your code in a much more readable fashion (without changing anything about the semantics):
bubble_sort(L) when length(L) =< 1 ->
L;
bubble_sort(L) ->
SL = bubble_sort_p(L),
bubble_sort(lists:sublist(SL,1,length(SL)-1)) ++ [lists:last(SL)].
bubble_sort_p([]) ->
[];
bubble_sort_p([F]) ->
[F];
bubble_sort_p([F,G|T]) when F > G ->
[G|bubble_sort_p([F|T])];
bubble_sort_p([F,G|T]) ->
[F|bubble_sort_p([G|T])].
This should allow for much easier contemplation of the things actually going on in the program.

And employing lists:foldr:
bubble(List) ->
Step = fun
(E,{S,[]}) -> {S,[E]};
(E,{_,[X|XS]}) when E > X -> {swapped,[X|[E|XS]]};
(E,{S,XS}) -> {S,[E|XS]}
end,
case lists:foldr(Step, {intact,[]}, List) of
{intact,XS} -> XS;
{swapped,XS} -> bubble(XS)
end.

There is an answer at http://en.literateprograms.org/Bubble_sort_%28Erlang%29
-module(bubblesort).
-export([sort/1]).
-import(lists, [reverse/1]).
sort(L) -> sort(L, [], true).
sort([], L, true) -> reverse(L);
sort([], L, false) -> sort(reverse(L), [], true);
sort([ X, Y | T ], L, _) when X > Y ->
sort([ X | T ], [ Y | L ], false);
sort([ X | T ], L, Halt) -> sort(T, [ X | L ], Halt).
An example for sorting 2,4,3,5,1
sort([2,4,3,5,1])
round 1
=> sort([2,4,3,5,1], [], true)
=> sort([4,3,5,1], [2], true)
=> sort([4,5,1], [3,2], false)
=> sort([5,1], [4,3,2], false)
=> sort([5], [1,4,3,2], false)
=> sort([], [5,1,4,3,2], false)
=> sort([2,3,4,1,5], [], true)
round 2
=> sort([3,4,1,5], [2], true)
=> sort([4,1,5], [3,2], true)
=> sort([4,5], [1,3,2], false)
=> sort([5], [4,1,3,2], false)
=> sort([], [5,4,1,3,2], false)
=> sort([2,3,1,4,5], [], true)
round 3
=> sort([3,1,4,5], [2], true)
=> sort([3,4,5], [1,2], false)
=> sort([4,5], [3,1,2], false)
=> sort([5], [4,3,1,2], false)
=> sort([], [5,4,3,1,2], false)
=> sort([2,1,3,4,5], true)
round 4
=> sort([2,3,4,5], [1], false)
=> sort([3,4,5], [2,1], false)
=> sort([4,5], [3,2,1], false)
=> sort([5], [4,3,2,1], false)
=> sort([], [5,4,3,2,1], false)
=> sort([1,2,3,4,5], [], true)
round 5
=> sort([2,3,4,5], [1], true)
=> sort([3,4,5],[2,1], true)
=> sort([4,5],[3,2,1], true)
=> sort([5],[4,3,2,1], true)
=> sort([], [5,4,3,2,1], true)
=> [1,2,3,4,5]

-module(bubbleSort).
-compile(export_all).
-include_lib("eunit/include/eunit.hrl").
sort(L) ->
sort(L, length(L), []).
sort(_L, 0, _Res) -> [];
sort([H | _T], 1, Res) -> [H | Res];
sort(L, Len, Res) ->
T1 = lists:sublist(L, 1, Len),
T2 = inner_sort(T1, []),
Last = lists:last(T2),
sort(T2, Len - 1, [Last | Res]).
inner_sort([A, B], Res) when (A < B)->
Res ++ [A, B];
inner_sort([A, B], Res) ->
Res ++ [B, A];
inner_sort([A, B | T], Res) when (A < B) ->
inner_sort([B | T], Res ++ [A]);
inner_sort([A, B | T], Res) ->
inner_sort([A | T], Res ++ [B]).
test()->
L = [5, 3, -1, 10, 6, 100, 99],
?assert(sort([]) =:= []),
?assert(sort([1]) =:= [1]),
?assert(sort([1, 2, 3, 4]) =:= [1, 2, 3, 4]),
?assert(sort([10, 5, 3, 2, 1]) =:= [1, 2, 3, 5, 10]),
?assert(sort(L) =:= [-1, 3, 5, 6, 10, 99, 100]).

Easy to read & understand implementation w ascending sort direction.
Tail recursion employed, computational complexity & memory usage are classic.
bsort([]) -> [];
bsort([H|T]) -> bsort([H|T],[]).
bsort([],[]) -> [];
bsort([],[H|T]) -> [H|T];
bsort([A|B], Sorted) when is_list(Sorted) -> M = lists:max([A|B]), bsort([A|B] -- [M], [M] ++ Sorted).

Related

Function to convert map to a list in erlang

Suppose I have a map like this
A = #{a=>1,b=>2,c=>3}.
I want to create a function which converts A to a list of tuples of key-value pairs.
list = [{a,1},{b,2},{c,3}]
maps:to_list/1 does exactly this:
1> maps:to_list(#{a=>1,b=>2,c=>3}).
[{a,1},{b,2},{c,3}]
You can use maps:fold/3 for loop map items. Let's say you need just convert a map, then you can use something like:
1> A = #{a=>1,b=>2,c=>3}.
2> maps:fold(
fun(K, V, Acc) ->
[{K, V} | Acc]
end,
[], A).
[{c,3},{b,2},{a,1}]
For case if need to do the same for nested maps, this example can be modify like:
1> A = #{a => 1, b => 2, c => 3, d => #{a => 1, b => #{a => 1}}},
2> Nested =
fun F(K, V = #{}, Acc) -> [{K, maps:fold(F, [], V)} | Acc];
F(K, V, Acc) -> [{K, V} | Acc]
end,
3> maps:fold(Nested, [], A).
[{d,[{b,[{a,1}]},{a,1}]},{c,3},{b,2},{a,1}]

return first {Key, Value} from Map where Predicate is true

The more formal definition of problem is
Write a function map_search_pred(Map, Pred) that returns the first
element {Key,Value} in the map for which Pred(Key, Value) is true.
My attempt, looks like
map_search_pred(Map, Pred) ->
M = [{Key, Value} || {Key, Value} <- maps:to_list(Map), Pred(Key, Value) =:= true],
case length(M) of
0 -> {};
_ -> lists:nth(1, M)
end.
When I run this, I get correct answer
1> lib_misc:map_search_pred(#{}, fun(X, Y) -> X =:= Y end).
{}
2> lib_misc:map_search_pred(#{1 => 1, 2 => 2}, fun(X, Y) -> X =:= Y end).
{1,1}
3> lib_misc:map_search_pred(#{1 => 1, 2 => 3}, fun(X, Y) -> X =:= Y end).
{1,1}
4> lib_misc:map_search_pred(#{1 => 2, 2 => 2}, fun(X, Y) -> X =:= Y end).
{2,2}
5>
Problem?
The problem is efficiency.
It uses list comprehension and runs for all the elements in the list. The better solution would be to return after the first match.
As a beginner to language, I do not know a better idiomatic way to solve this, so looking for better ideas and suggestions
You could walk the list yourself and return as soon as you find an element for which Pred(Key, Value) is true:
map_search_pred(Map, Pred) when is_map(Map) ->
map_search_pred(maps:to_list(Map), Pred);
map_search_pred([], _) ->
error;
map_search_pred([{Key,Value}=H|T], Pred) ->
case Pred(Key, Value) of
true ->
{ok, H};
false ->
map_search_pred(T, Pred)
end.

Empty Map Pattern matches even for non-empty Map

The problem I am trying to solve states
Write a function map_search_pred(Map, Pred) that returns the first
element {Key,Value} in the map for which Pred(Key, Value) is true.
My attempt looks like
map_search_pred(#{}, _) -> {};
map_search_pred(Map, Pred) ->
[H|_] = [{Key, Value} || {Key, Value} <- maps:to_list(Map), Pred(Key, Value) =:= true],
H.
When I run this, I see output as
1> lib_misc:map_search_pred(#{1 => 1, 2 => 3}, fun(X, Y) -> X =:= Y end).
{}
2> lib_misc:map_search_pred(#{1 => 1, 2 => 3}, fun(X, Y) -> X =:= Y end).
{}
3> maps:size(#{}).
0
4>
How am I so sure?
I pulled out the first clause so it looks like
map_search_pred(Map, Pred) ->
[H|_] = [{Key, Value} || {Key, Value} <- maps:to_list(Map), Pred(Key, Value) =:= true],
H.
and run again
1> lib_misc:map_search_pred(#{1 => 1, 2 => 3}, fun(X, Y) -> X =:= Y end).
{1,1}
2> lib_misc:map_search_pred(#{}, fun(X, Y) -> X =:= Y end).
** exception error: no match of right hand side value []
in function lib_misc:map_search_pred/2 (/Users/harith/code/IdeaProjects/others/erlang/programmingErlang/src/lib_misc.erl, line 42)
3>
According to map documentation:
Matching an expression against an empty map literal will match its type but no variables will be bound:
#{} = Expr
This expression will match if the expression Expr is of type map, otherwise it will fail with an exception badmatch.
However erlang:map_size can be used instead:
map_search_pred(Map, _) when map_size(Map) == 0 ->
{};
map_search_pred(Map, Pred) ->
[H|_] = [{Key, Value} || {Key, Value} <- maps:to_list(Map), Pred(Key, Value) =:= true],
H.

Combine/Merge Two Erlang lists

How to combine tuple lists in erlang? I have lists:
L1 = [{k1, 10}, {k2, 20}, {k3, 30}, {k4, 20.9}, {k6, "Hello world"}],
and
L2 = [{k1, 90}, {k2, 210}, {k3, 60}, {k4, 66.9}, {k6, "Hello universe"}],
now I want a combined list as :
L3 = [
{k1, [10, 90]},
{k2, [20, 210]},
{K3, [30, 60]},
{k4, [20.9, 66.9]},
{K6, ["Hello world", "Hello universe"]}
].
There is a nice solution to this one by using the sofs module in the Erlang Standard library. The sofs module describes a DSL for working with mathematical sets. This is one of those situations, where you can utilize it by transforming your data into the SOFS-world, manipulate them inside that world, and then transform them back again outside afterwards.
Note that I did change your L3 a bit, since sofs does not preserve the string order.
-module(z).
-compile(export_all). % Don't do this normally :)
x() ->
L1 = [{k1, 10}, {k2, 20}, {k3, 30}, {k4, 20.9}, {k6, "Hello world"}],
L2 = [{k1, 90}, {k2, 210}, {k3, 60}, {k4, 66.9}, {k6, "Hello universe"}],
L3 = [{k1, [10, 90]},{k2, [20, 210]},{k3, [30, 60]},{k4, [20.9, 66.9]},{k6, ["Hello universe", "Hello world"]}],
R = sofs:relation(L1 ++ L2),
F = sofs:relation_to_family(R),
L3 = sofs:to_external(F),
ok.
Something shorter, and the lists don't even have to posses the same keys, and can be unordered:
merge(In1,In2) ->
Combined = In1 ++ In2,
Fun = fun(Key) -> {Key,proplists:get_all_values(Key,Combined)} end,
lists:map(Fun,proplists:get_keys(Combined)).
Fun could be written directly in the lists:map/2 function, but this makes it readable.
Output, with data from example:
1> test:merge(L1,L2).
[{k1,"\nZ"},
{k2,[20,210]},
{k3,[30,60]},
{k4,[20.9,66.9]},
{k6,["Hello world","Hello universe"]}]
"\nZ" is because erlang interprets [10,90] as a string (which are, in fact, lists). Don't bother.
This technique is called merge join. It is well known in database design.
merge(L1, L2) ->
merge_(lists:sort(L1), lists:sort(L2)).
merge_([{K, V1}|T1], [{K, V2}|T2]) -> [{K, [V1, V2]}|merge_(T1, T2)];
merge_([], []) -> [].
If there can be different sets of keys in both lists and you are willing to drop those values you can use
merge_([{K, V1}|T1], [{K, V2}|T2]) -> [{K, [V1, V2]}|merge_(T1, T2)];
merge_([{K1, _}|T1], [{K2, _}|_]=L2) when K1 < K2 -> merge_(T1, L2);
merge_(L1, [{_, _}|T2]) -> merge_(L1, T2);`
merge_(_, []) -> [].
Or if you would like store those values in lists
merge_([{K, V1}|T1], [{K, V2}|T2]) -> [{K, [V1, V2]}|merge_(T1, T2)];
merge_([{K1, V1}|T1], [{K2, _}|_]=L2) when K1 < K2 -> [{K1, [V1]}|merge_(T1, L2)];
merge_(L1, [{K2, V2}|T2]) -> [{K2, [V2]}|merge_(L1, T2)];
merge_(L1, []) -> [{K, [V]} || {K, V} <- L1].
You can of course use tail recursive version if you don't mind result in reverse order or you can always use lists:reverse/1
merge(L1, L2) ->
merge(lists:sort(L1), lists:sort(L2), []).
merge([{K, V1}|T1], [{K, V2}|T2], Acc) -> merge(T1, T2, [{K, [V1, V2]}|Acc]);
merge([], [], Acc) -> Acc. % or lists:reverse(Acc).
Or
merge([{K, V1}|T1], [{K, V2}|T2], Acc) -> merge(T1, T2, [{K, [V1, V2]}|Acc]);
merge([{K1, _}|T1], [{K2, _}|_]=L2, Acc) when K1 < K2 -> merge(T1, L2, Acc);
merge(L1, [{_, _}|T2], Acc) -> merge(L1, T2, Acc);`
merge(_, [], Acc) -> Acc. % or lists:reverse(Acc).
Or
merge([{K, V1}|T1], [{K, V2}|T2], Acc) -> merge(T1, T2, [{K, [V1, V2]}|Acc]);
merge([{K1, V1}|T1], [{K2, _}|_]=L2, Acc) when K1 < K2 -> merge(T1, L2, [{K1, [V1]}|Acc]);
merge(L1, [{K2, V2}|T2], Acc) -> merge(L1, T2, [{K2, [V2]}|Acc]);`
merge([{K1, V1}|T1], [], Acc) -> merge(T1, [], [{K1, [V1]} | Acc]);
merge([], [], Acc) -> Acc. % or lists:reverse(Acc).
% or merge(L1, [], Acc) -> lists:reverse(Acc, [{K, [V]} || {K, V} <- L1]).
% instead of two last clauses.
If there is possibility that one of lists can contain same keys and you are willing collect all values you can consider this
merge(L1, L2) ->
merge(lists:sort(L1), lists:sort(L2), []).
merge([{K1, _}|_]=L1, {K2, _}|_]=L2, Acc) ->
K = min(K1, K2),
{Vs1, T1} = collect(K, L1, []),
{Vs2, T2} = collect(K, L2, Vs1),
merge(T1, T2, [{K, Vs2}|Acc]);
merge([{K, _}|_]=L1, [], Acc) ->
{Vs, T1} = collect(K, L1, []),
merge(T1, [], [{K, Vs}|Acc]);
merge([], [{K, _}|_]=L2, Acc) ->
{Vs, T2} = collect(K, L2, []),
merge([], T2, [{K, Vs}|Acc]);
merge([], [], Acc) -> lists:reverse(Acc).
collect(K, [{K, V}|T], Acc) -> collect(K, T, [V|Acc]);
collect(_, T, Acc) -> {Acc, T}.
What happened to lists:zipwith/2?
Assumptions:
lists are the same length
lists contain the same keys in the same order
lists:zipwith(fun({X, Y}, {X, Z}) -> {X, [Y, Z]} end, L1, L2).
Maybe this is not the best way, but it does what you are trying to achieve.
merge([{A, X}| T1], [{A, Y} | T2], Acc) ->
New_acc = [{A, [X, Y]} | Acc],
merge(T1, T2, New_acc);
merge([{A, X} | T1], [{B, Y} | T2], Acc) ->
New_acc = [{A, [X]}, {B, Y} | Acc],
merge(T1, T2, New_acc);
merge([], [{B, Y} | T], Acc) ->
New_acc = [{B, Y} | Acc],
merge([], T, New_acc);
merge([{A, X} | T], [], Acc) ->
New_acc = [{A, X} | Acc],
merge(T, [], New_acc);
merge([], [], Acc) ->
lists:reverse(Acc).
Edit
I'm assuming that the input lists are ordered as in your sample input. If not you can use lists:sort/2 to sort them before merging.
You can use lists comprehensions:
L1 = [{k1, 10}, {k2, 20}, {k3, 30}, {k4, 20.9}, {k6, "Hello world"}],
L2 = [{k1, 90}, {k2, 210}, {k3, 60}, {k4, 66.9}, {k6, "Hello universe"}],
[ {K,[V1,V2]} || {K,V1} <- L1, {K2,V2} <- L2, K == K2].
Output:
[{k1,"\nZ"}, % [10,90] shown in console as string..
{k2,[20,210]},
{k3,[30,60]},
{k4,[20.9,66.9]},
{k6,["Hello world","Hello universe"]}]

How to compare two lists of rules?

I need to compare two Lists of Rules of form var -> integer on the fact of mismatch.
To determine, if there are any rules the same by lhs and different by rhs.
For example:
{a->3, b->1, c->4} ~ ??? ~ {a->3, b->1, c->4} = true
{a->3, b->1, c->4} ~ ??? ~ {a->3, b->2, c->4} = false
{a->3, b->1, c->4} ~ ??? ~ {a->1, b->3, c->4} = false
{a->3, b->1, c->4} ~ ??? ~ {c->4, d->8, e->9} = true
{a->3, b->1, c->4} ~ ??? ~ {d->8, e->9, f->7} = true
In my case they are already sorted by lhs and all lhs are unique if it could help to make as simple function as possible.
UPD: forgot one thing! Lists can be of different length. But seems like all three current answers are still valid.
Here's another solution:
In[12]:= check[a:{__Rule}, b:{__Rule}] := FilterRules[a, b] === FilterRules[b, a]
In[18]:= {{a -> 3, b -> 1, c -> 4}~check ~ {a -> 3, b -> 1, c -> 4} ,
{a -> 3, b -> 1, c -> 4}~check ~ {a -> 3, b -> 2, c -> 4},
{a -> 3, b -> 1, c -> 4}~check ~ {a -> 1, b -> 3, c -> 4},
{a -> 3, b -> 1, c -> 4}~check ~ {c -> 4, d -> 8, e -> 9},
{a -> 3, b -> 1, c -> 4}~check ~ {d -> 8, e -> 9, f -> 7}}
Out[18]= {True, False, False, True, True}
(This relies on the fact that the lists of options are already sorted.)
You could do something like
check[{a__Rule}, {b__Rule}] :=
Module[{common = Intersection[{a}[[All, 1]], {b}[[All, 1]]]},
SameQ[common /. {a}, common /. {b}]]
Then
check[{a -> 3, b -> 1, c -> 4}, {a -> 3, b -> 1, c -> 4}]
check[{a -> 3, b -> 1, c -> 4}, {a -> 3, b -> 2, c -> 4}]
check[{a -> 3, b -> 1, c -> 4}, {a -> 1, b -> 3, c -> 4}]
yields
True
False
False
Perhaps simpler
check[a : {__Rule}, b : {__Rule}] := SameQ ## Transpose[a /. b /. a /. Rule -> List]
EDIT
Here is an even more esoteric version, which has the advantage of being completely high-level, in the sense that we don't need to know anything about the internal structure of the rules, only how they act:
checkAlt[a : {__Rule}, b : {__Rule}] := # === (# /. #) &[a /. b /. a]
EDIT 2
Well, let me throw in another one, just for fun:
check1[{a__Rule}, {b__Rule}] := SameQ ## ({a, b} /. {{a, b}, {b, a}})
Here's a slightly generalized approach:
In[24]:= check[lists__] :=
And ## (SameQ ### GatherBy[Join[lists], First])
In[25]:= {
{a -> 3, b -> 1, c -> 4}~check~{a -> 3, b -> 1, c -> 4},
{a -> 3, b -> 1, c -> 4}~check~{a -> 3, b -> 2, c -> 4},
{a -> 3, b -> 1, c -> 4}~check~{a -> 1, b -> 3, c -> 4},
{a -> 3, b -> 1, c -> 4}~check~{c -> 4, d -> 8, e -> 9},
{a -> 3, b -> 1, c -> 4}~check~{d -> 8, e -> 9, f -> 7}
}
Out[25]= {True, False, False, True, True}
This one doesn't require the elements to be rules, they could be lists, or pretty much any other head. It should also work on any number of inputs.

Resources