Skip to content

Instantly share code, notes, and snippets.

@ericdouglas
Last active May 8, 2020 13:40
Show Gist options
  • Save ericdouglas/4ed3a2b9169c8e8f58ddf3572e23b832 to your computer and use it in GitHub Desktop.
Save ericdouglas/4ed3a2b9169c8e8f58ddf3572e23b832 to your computer and use it in GitHub Desktop.
Functional Programmin with Erlang exercises
-module(first).
-export([area/3, double/1, mult/2, square/1, treble/1]).
mult(X, Y) -> X * Y.
double(X) -> mult(2, X).
area(A, B, C) ->
S = (A + B + C) / 2,
math:sqrt(S * (S - A) * (S - B) * (S - C)).
treble(X) -> mult(3, X).
square(X) -> mult(X, X).
-module(second).
-export([find_hypotenuse/2, triangle_area/2,
triangle_perimeter/2]).
find_hypotenuse(X, Y) ->
math:sqrt(math:pow(X, 2) + math:pow(Y, 2)).
triangle_perimeter(X, Y) ->
Hipotenuse = find_hypotenuse(X, Y), X + Y + Hipotenuse.
triangle_area(X, Y) -> first:mult(X, Y) / 2.
% Exercises from "Variables and patterns in practice" section
-module(third).
-export([count/2, count_occurrences_in_list/1,
how_many_equal/3, how_many_translator/1, max_three/3,
test_count/0, test_count_occurrences_in_list/0,
test_how_many_equal/0, test_how_many_translator/0,
test_max_three/0]).
% Maximum of three
% Give a definition of the function max_three which takes three integers
% and returns the maximum of the three. You can use the max function,
% which gives the maximum of two numbers, in writing your definition.
% max_three(34,25,36) = 36
max_three(X, Y, Z) ->
lists:foldl(fun (Elem, Acc) -> max(Elem, Acc) end, X,
[X, Y, Z]).
test_max_three() -> max_three(34, 36, 25) == 36.
% How many equal?
% Give a definition of the function how_many_equal which takes three integers
% and returns an integer, counting how many of its three arguments are equal, so that:
% how_many_equal(34,25,36) = 0
% how_many_equal(34,25,34) = 2
% how_many_equal(34,34,34) = 3
% SOLUTION: steps
% 1. create a list without repeated items
% 2. map this list and find the occurrence of each number in the original list
% 3. reduce the occurence list: if a number appeared once, sum 0, otherwise, sum the occurence
how_many_equal(X, Y, Z) ->
List = [X, Y, Z],
Uniques = sets:to_list(sets:from_list(List)),
Occurences = lists:map(count_occurrences_in_list(List),
Uniques),
lists:foldl(fun (Elem, Acc) ->
Acc + how_many_translator(Elem)
end,
0, Occurences).
test_how_many_equal() ->
[how_many_equal(34, 25, 36) == 0,
how_many_equal(34, 25, 34) == 2,
how_many_equal(34, 34, 34) == 3]
== [true, true, true].
count(_, []) -> 0;
count(X, [X | Tail]) -> 1 + count(X, Tail);
count(X, [_ | Tail]) -> count(X, Tail).
test_count() -> count(7, [1, 7, 3, 7, 5, 7]) == 3.
count_occurrences_in_list(List) ->
fun (X) -> count(X, List) end.
test_count_occurrences_in_list() ->
List = [1, 2, 3, 2, 3, 4, 2, 5],
(count_occurrences_in_list(List))(2) == 3.
how_many_translator(X) ->
case X > 1 of
true -> X;
false -> 0
end.
test_how_many_translator() ->
[how_many_translator(1) == 0,
how_many_translator(3) == 3]
== [true, true].
@elbrujohalcon
Copy link

This is a good solution, but the idea was to use first:square/1 and not math:pow/2 ;)

@ericdouglas
Copy link
Author

@elbrujohalcon yes, I just like to learn/check the API and docs of the language I'm currently learning hehe Thanks for the comment, though.

@elbrujohalcon
Copy link

@ericdouglas You came up with a super-advanced solution for the exercise. Kudos for that!
A few tips, but the goal of the exercise was to practice pattern-matching in a simple way.
High-order functions and recursion over lists will come… soon :)

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