Skip to content

Instantly share code, notes, and snippets.

@sn1p3r46
Last active November 16, 2016 00:25
Show Gist options
  • Save sn1p3r46/a5bf5953c264d6d79ac3a39996cfff23 to your computer and use it in GitHub Desktop.
Save sn1p3r46/a5bf5953c264d6d79ac3a39996cfff23 to your computer and use it in GitHub Desktop.
Erlang functional programming exercises;
%%% Exercises description on comments;
-module(exercises0).
-export([count/1,repeated_elems/1,most_frequent_elem/1,ntimes/3]).
%%% #1
count([H|T]) ->
count([H|T],[]);
count([]) -> [].
count([],L) -> L;
count([H|T],L) ->
count(T,count(H,L,[])).
count(V,[ {C,K} | T ],L) ->
case K of
V -> L ++ [{C+1,K} | T];
_ -> count(V,T,L ++ [{C,K}])
end;
count(V,[],L) -> L ++ [{1,V}].
%%% #2
most_frequent_elem([H|T]) ->
most_frequent_elem(count([H|T]),{0,0}).
most_frequent_elem([{C0,K0}|T],{C,K}) ->
case (C0>=C) of
true -> most_frequent_elem(T,{C0,K0});
false -> most_frequent_elem(T,{C,K})
end;
most_frequent_elem([],{_,K})->K.
%%% #3
repeated_elems([H|T]) ->
[ K || {C,K} <- count([H|T]) , C>1].
%%% #4
ntimes(F,N,EList) ->
[ asd(F,N,V) || V <- EList ].
asd(_,0,R) -> R;
asd(F,N,R) ->
asd(F,N-1,F(R)).
@sn1p3r46
Copy link
Author

sn1p3r46 commented Nov 16, 2016

Exercises0

#1 Count function

Define the count function that finds all repeated elements in a list and packs them together with the number of their occurrences as pairs.

count(list()) -> list(tuple(int(), term()))

Test cases:

    count([one, two, one, two, 3, 4,2,2]) == [{2,2},{1,3},{1,4},{2,one},{2,two}]
    count("apple") == [{1,97},{1,101},{1,108},{2,112}]
    count("apple") == [{1,$a},{1, $e},{1, $l}, {2, $p}]
    count("") == []

#2 Most frequent elements

Define the most_frequent_elem/1 function that returns which element occurs the most in a list.

most_frequent_elem(list())->term()

Test cases:

    most_frequent_elem([one, two, one]) == one
    most_frequent_elem([]) = empty_list
    most_frequent_elem([1,2]) == 2
    most_frequent_elem([1,2, apple, apple]) == apple

#3 Repeated elements

Define the repeated_elems/1 function that determines what elements occur more than once in a list.

repeated_elems(L::list()) -> list()

Test cases:

    repeated_elems([1,2, apple, apple])== [apple]
    repeated_elems([]) == []
    repeated_elems([one, two, one, two, 3, 4,4,44,2,2]) == [two,one,4,2]

#4 Applying a function n times

Define the ntimes/3 function that applies the F function on each element of the given list N times.

ntimes(F::function(), N::integer(), EList::list()) -> list()

If you have to apply function f/1 3 times and EList is [1,2], then the return value of the function defined as: [f(f(f(1))), f(f(f(2)))]

    ntimes(fun(E) -> E*2 end, 3, [1,2,3,4]) == [8,16,24,32]
    ntimes(fun(E) -> E*2 end, 3, []) == []
    ntimes(fun(E) -> E*2 end, 0, [1,2]) == [1,2]
    ntimes(fun math:sqrt/1, 2, [27, 16, 81]) == [2.2795070569547775,2.0,3.0]

Andrea Galloni

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment