Last active
May 7, 2020 13:51
-
-
Save sgobotta/602ecca7e8e21512e74d28511cb953f7 to your computer and use it in GitHub Desktop.
Gist for the FeatureLearn Erlang course (1.15)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% @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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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'. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Common Test ;)