Skip to content

Instantly share code, notes, and snippets.

@sgobotta
Last active May 7, 2020 13:51
Show Gist options
  • Save sgobotta/602ecca7e8e21512e74d28511cb953f7 to your computer and use it in GitHub Desktop.
Save sgobotta/602ecca7e8e21512e74d28511cb953f7 to your computer and use it in GitHub Desktop.
Gist for the FeatureLearn Erlang course (1.15)
% @doc Pattern Mathing module for the 1.15 step of the FutureLearn Erlang Course.
-module(pattern_matching).
-export([
xOr/2,
xOr1/2,
xOr2/2,
xOr3/2,
xOr4/2,
max_three/3,
how_many_equal/3
]).
% Exclusive Or from lesson
xOr(X,X) -> false;
xOr(_,_) -> true.
% Exclusive Or implementations
xOr1(true,true) -> false;
xOr1(false,false) -> false;
xOr1(_,_) -> true.
xOr2(true, false) -> true;
xOr2(false, true) -> true;
xOr2(_,_) -> false.
xOr3(X,Y) -> X =/= Y.
xOr4(X,Y) -> not X == Y.
% @doc Given three integers returns the maximum
max_three(X,Y,Z) -> max(X, max(Y,Z)).
% @doc Given three integers returns a number representing the equivalent ones
how_many_equal(X,X,X) -> 3;
how_many_equal(X,X,_) -> 2;
how_many_equal(X,_,X) -> 2;
how_many_equal(_,X,X) -> 2;
how_many_equal(_,_,_) -> 0.
-module(test_pattern_matching).
-export([
test_max_three/0,
test_how_many_equal/0
]).
test_max_three() ->
4 = pattern_matching:max_three(4, 2, 0),
4 = pattern_matching:max_three(2, 4, 0),
4 = pattern_matching:max_three(0, 2, 4),
':: max_three tests passed succesfully'.
test_how_many_equal() ->
3 = pattern_matching:how_many_equal(4,4,4),
2 = pattern_matching:how_many_equal(4,4,0),
2 = pattern_matching:how_many_equal(4,0,4),
2 = pattern_matching:how_many_equal(0,4,4),
0 = pattern_matching:how_many_equal(4,2,0),
':: how_many_equal tests passed succesfully'.
@elbrujohalcon
Copy link

It looks nice! If you want to comply with common_test conventions (which might be useful for you in the future) your tests should return something like…

{comment, ""}.

But since you're not using CT yet… it doesn't matter.

@sgobotta
Copy link
Author

sgobotta commented May 7, 2020

It looks nice! If you want to comply with common_test conventions (which might be useful for you in the future) your tests should return something like…

{comment, ""}.

But since you're not using CT yet… it doesn't matter.

What does CT stand for?

@elbrujohalcon
Copy link

elbrujohalcon commented May 7, 2020

What does CT stand for?

Common Test ;)

@sgobotta
Copy link
Author

sgobotta commented May 7, 2020

What does CT stand for?

Common Test ;)

Thanks @elbrujohalcon 👍 !

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