Erlang: "prepending" an element to a tuple - erlang

Is it possible to write a faster equivalent to this function?
prepend(X, Tuple) ->
list_to_tuple([X | tuple_to_list(Tuple)]).

It looks to me like that sort of thing is discouraged. If you want a list, use one.
Getting Started with Erlang:
Tuples have a fixed number of things in them.

If you have a finite number of possible tuple lengths, you could do this:
prepend(X, {}) -> {X};
prepend(X, {A}) -> {X, A};
prepend(X, {A, B}) -> {X, A, B};
prepend(X, {A, B, C}) -> {X, A, B, C}.
You can continue this pattern for as long as you need.

As prepending an element is the same as inserting it at position 1, you can use the built-in function erlang:insert_element/3:
> erlang:insert_element(1, {a, b}, z).
{z,a,b}
This function was added in Erlang/OTP R16A.

Related

Erlang: serial implementation of accumulator

I am trying to create a method that takes an associative and commutative operator, as well a list of values, and then returns the answer by applying an operator to the values in the list.
The following two examples represent what the input/output are supposed to look like.
Example 1
Input: sum(fun(A,B) -> A+B end, [2,6,7,10,12]).
Output: 37
Example 2
Input: sum(fun (A,B) -> A++B end , ["C", "D", "E"]).
Output: "CDE"
This is the code I am working with so far.
-module(tester).
-compile(export_all).
sum(Func, Data, Acc) ->
lists:foldr(Func, Acc, Data).
This code produces the correct result, however, there are two problems I am trying to figure out how to approach answering.
(1) In order for this code to work, it requires an empty list to be included at the end of the command line statements. In other words, if I enter the input above (as in the examples), it will err out, because I did not write it in the following way:
12> tester:sum(fun(X, Acc) -> X+Acc end, [2,6,7,10,12], 0).
How would I implement this without an empty list as in the examples above and get the same result?
(2) Also, how would the code be implemented without the list function, or in an even more serial way?
How would I implement this without an empty list as in the examples above and get the same result?
Assuming the list always has one element (you can't really do it without this assumption), you can extract the first element from the list and pass that as the initial accumulator. You'll need to switch to foldl to do this efficiently. (With foldr you'll essentially need to make a copy of the list to drop the last element.)
sum(Func, [X | Xs]) ->
lists:foldl(fun (A, B) -> Func(B, A) end, X, Xs).
1> a:sum(fun(A,B) -> A+B end, [2,6,7,10,12]).
37
2> a:sum(fun (A,B) -> A++B end , ["C", "D", "E"]).
"CDE"
Also, how would the code be implemented without the list function, or in an even more serial way?
Here's a simple implementation using recursion and pattern matching:
sum2(Func, [X | Xs]) ->
sum2(Func, Xs, X).
sum2(Func, [], Acc) ->
Acc;
sum2(Func, [X | Xs], Acc) ->
sum2(Func, Xs, Func(Acc, X)).
We define two versions of the function. The first one extracts the head and uses that as the initial accumulator. The second one, with arity 3, does essentially what the fold functions in lists do.
After working on this for a while, this was my solution. I've left some comments about the general idea of what I did, but there's a lot more to be said.
-module(erlang2).
-compile(export_all).
-export([reduce/2]).
reduce(Func, List) ->
reduce(root, Func, List).
%When done send results to Parent
reduce(Parent, _, [A]) ->
%send to parent
Parent ! { self(), A};
%I tried this at first to take care of one el in list, but it didn't work
%length ([]) ->
% Parent ! {self(), A};
%get contents of list, apply function and store in Parent
reduce(Parent, Func, List) ->
{ Left, Right } = lists:split(trunc(length(List)/2), List),
Me = self(),
%io:format("Splitting in two~n"),
Pl = spawn(fun() -> reduce(Me, Func, Left) end),
Pr = spawn(fun() -> reduce(Me, Func, Right) end),
%merge results in parent and call Func on final left and right halves
combine(Parent, Func,[Pl, Pr]).
%merge pl and pl and combine in parent
combine(Parent, Func, [Pl, Pr]) ->
%wait for processes to complete (using receive) and then send to Parent
receive
{ Pl, Sorted } -> combine(Parent, Func, Pr, Sorted);
{ Pr, Sorted } -> combine(Parent, Func, Pl, Sorted)
end.
combine(Parent, Func, P, List) ->
%wait and store in results and then call ! to send
receive
{ P, Sorted } ->
Results = Func(Sorted, List),
case Parent of
root ->
Results;
%send results to parent
_ -> Parent ! {self(), Results}
end
end.

Extend one list of key-value pairs with another one

I have two lists of key-value pairs ([{key_1, value_1}, ..., {key_n, value_n}]). What is the best method of updating first list with the second one? For example:
1> extend([{1, "one"}, {2, "too"}], [{2, "two"}, {3, "three"}]).
[{1, "one"}, {2, "two"}, {3, "three"}]
I've found just two similar function: lists:keystore/4, which updates a single tuple, and lists:keymerge, which merges two lists without removing key duplicates.
I've found the answer myself. Erlang's orddict module deals with pure sorted list of {key, value} pairs. So, extend function can be defined as follows:
extend(L1, L2) ->
orddict:merge(fun(_Key, _V1, V2) -> V2 end, L1, L2).
If L1 and L2 aren't initially sorted, then they can be transformed to orddicts with orddict:from_list/1.
for instance
extend(L1,L2) ->
lists:foldl(fun({K,V},Acc) -> lists:keystore(K,1,Acc,{K,V}) end, L1,L2).
or
extend(L1,L2) ->
compact(lists:keysort(1,L1++L2),[]).
compact([],Acc) -> lists:reverse(Acc);
compact([{K,_},{K,V}| Rest], Acc) -> compact([{K,V} |Rest],Acc);
compact([X|Rest],Acc) -> compact(Rest,[X|Acc]).

Erlang, replacing an atom with another one in a list

I want to write a function to replace a specific atom with the given atom in an input list. But I want to do it using pattern matching and not using conditional statements. Any idea?
And also I want to write a function to return unique atoms in an expression.
e.g.
Input:
[a, b, c, a, b]
Output:
c
Input:
[b, b, b, r, t, y, y]
Output:
[t, r]
Assuming you want to replace all instances and keep the order of the list (works with all terms):
replace(Old, New, List) -> replace(Old, New, List, []).
replace(_Old, _New, [], Acc) -> lists:reverse(Acc);
replace(Old, New, [Old|List], Acc) -> replace(Old, New, List, [New|Acc]);
replace(Old, New, [Other|List], Acc) -> replace(Old, New, List, [Other|Acc]).
For the unique elements filter, you need to keep a state of which elements you have looked at already.
It would be really awkward to implement such a function using only pattern matching in the function headers and you would not really gain anything (performance) from it. The awkwardness would come from having to loop through both the list in question and the list(s) keeping your state of already parsed elements. You would also loose a lot of readability.
I would recommend going for something simpler (works with all terms, not just atoms):
unique(List) -> unique(List, []).
unique([], Counts) ->
lists:foldl(fun({E, 1}, Acc) -> [E|Acc];
(_, Acc) -> Acc
end, [], Counts);
unique([E|List], Counts) ->
unique(List, count(E, Counts).
count(E, []) -> [{E, 1}];
count(E, [{E, N}|Rest]) -> [{E, N + 1}|Rest];
count(E, [{X, N}|Rest]) -> [{X, N}|count(E, Rest)].
One way I'm looking for solving your first question would be to use guards, instead of if statements. Using only pattern matching doesn't seem possible (or desirable, even if you can do it).
So, for instance, you could do something like:
my_replace([H|T], ToReplace, Replacement, Accum) when H == ToReplace ->
my_replace(T, ToReplace, Replacement, [Replacement|Accum]);
my_replace([H|T], ToReplace, Replacement, Accum) ->
my_replace(T, ToReplace, Replacement, [H|Accum]);
my_replace([], ToReplace, Replacement, Accum) ->
lists:reverse(Accum).
EDIT: Edited for simplicity and style, thanks for the comments. :)
For the second part of your question, what do you consider an "expression"?
EDIT: Nevermind that, usort doesn't completely remove duplicates, sorry.

Erlang un-zip-flatten

I have a list of items that I would like to "un-zip-flatten". Basically what that means is that if I have a list of items:
[a, b, c, d, e, f, g]
I want to turn it into a list of lists like the following:
[[a, d, g], [b, e], [c, f]]
So far my solution looks like this:
unzipflatten(NumberOfLists, List) ->
lists:map(fun(Start) ->
lists:map(fun(N) ->
lists:nth(N, List)
end,
lists:seq(Start, length(List), NumberOfLists))
end,
lists:seq(1, NumberOfLists)).
I'm pretty new to Erlang so I'm wondering if I've missed some standard library function that would do what I want, or if there's a more "Erlangish" way to do this, or if the performance of my above solution will stink.
I think this would be a more "Erlangish" method to do this. Basically you would create the list of lists that will be your result, and use two lists to manage those lists like a queue. The "Heads" list contains the lists that you will prepend to next, and the "Tails" list are the ones most recently prepended to. When Heads is empty you simply reverse Tails and use that as the new Heads. Before returning the result you will need to reverse all of the lists inside Tails and Heads and then append Heads as-is to the reversed Tails. Excuse the confusing variable names, I think coming up with several good names for breaking up lists in an Erlang program is the hardest part ;)
unzipflatten(NumberOfLists, List) when NumberOfLists > 0 ->
unzipflatten(List, lists:duplicate(NumberOfLists, []), []).
unzipflatten([], Heads, Tails) ->
[lists:reverse(L) || L <- lists:reverse(Tails, Heads)];
unzipflatten(L, [], Tails) ->
unzipflatten(L, lists:reverse(Tails), []);
unzipflatten([Elem | Rest], [Head | Tail], Tails) ->
unzipflatten(Rest, Tail, [[Elem | Head] | Tails]).
It's also possible to do the "unzip" phase in a non tail-recursive way to avoid the lists:reverse step, but that is a more complicated solution. Something like this:
unzipflatten(NumberOfLists, List) when NumberOfLists > 0 ->
unzipflatten({List, lists:duplicate(NumberOfLists, [])}).
unzipflatten({[], Heads}) ->
[lists:reverse(L) || L <- Heads];
unzipflatten({L, Heads}) ->
unzipflatten(unzipper({L, Heads})).
unzipper({[], Heads}) ->
{[], Heads};
unzipper({L, []}) ->
{L, []};
unzipper({[H | T], [Head | Tail]}) ->
{T1, Tail1} = unzipper({T, Tail}),
{T1, [[H | Head] | Tail1]}.
Yes, performance will stink (basic advice for using lists:nth: never call it several times with growing N!). Something like this should be better (not tested):
unzipflatten(NumberOfLists, List) ->
unzipflatten(NumberOfLists, List, array:new(NumberOfLists, {default, []}), 0).
unzipflatten(_, [], Lists, _) ->
lists:map(fun lists:reverse/1, array:to_list(Lists));
unzipflatten(NumberOfLists, [H | T], Lists, CurrentIndex) ->
NewLists = array:set(CurrentIndex, [H | array:get(CurrentIndex, Lists)], Lists),
unzipflatten(NumberOfLists, T, NewLists, (CurrentIndex + 1) rem NumberOfLists).

Overuse of guards in Erlang?

I have the following function that takes a number like 5 and creates a list of all the numbers from 1 to that number so create(5). returns [1,2,3,4,5].
I have over used guards I think and was wondering if there is a better way to write the following:
create(N) ->
create(1, N).
create(N,M) when N =:= M ->
[N];
create(N,M) when N < M ->
[N] ++ create(N + 1, M).
The guard for N < M can be useful. In general, you don't need a guard for equality; you can use pattern-matching.
create(N) -> create(1, N).
create(M, M) -> [M];
create(N, M) when N < M -> [N | create(N + 1, M)].
You also generally want to write functions so they are tail-recursive, in which the general idiom is to write to the head and then reverse at the end.
create(N) -> create(1, N, []).
create(M, M, Acc) -> lists:reverse([M | Acc]);
create(N, M, Acc) when N < M -> create(N + 1, M, [N | Acc]).
(Of course, with this specific example, you can alternatively build the results in the reverse order going down to 1 instead of up to M, which would make the lists:reverse call unnecessary.)
If create/2 (or create/3) is not exported and you put an appropriate guard on create/1, the extra N < M guard might be overkill. I generally only check on the exported functions and trust my own internal functions.
create(N,N) -> [N];
create(N,M) -> [N|create(N + 1, M)]. % Don't use ++ to prefix a single element.
This isn't quite the same (you could supply -5), but it behaves the same if you supply meaningful inputs. I wouldn't bother with the extra check anyway, since the process will crash very quickly either way.
BTW, you have a recursion depth problem with the code as-is. This will fix it:
create(N) ->
create(1, N, []).
create(N, N, Acc) -> [N|Acc];
create(N, M, Acc) -> create(N, M - 1, [M|Acc]).
I don't really think you have over used guards. There are two cases:
The first is the explicit equality test in the first clause of create/2
create(N, M) when N =:= M -> [M];
Some have suggested transforming this to use pattern matching like
create(N, N) -> [N];
In this case it makes no difference as the compiler internally transforms the pattern matching version to what you have written. You can safely pick which version you think feels best in each case.
In the second case you need some form of sanity check that the value of the argument in the range you expect it to be. Doing in every loop is unnecessary and I would move it to an equivalent test in create/1:
create(M) when M > 1 -> create(1, M).
If you want to use an accumulator I would personally use the count version as it saves reversing the list at the end. If the list is not long I think the difference is very small and you can pick the version which feels most clear to you. Anyway, it is very easy to change later if you find it to be critical.

Resources