Skip to content

Instantly share code, notes, and snippets.

@Joakineee
Last active May 8, 2020 06:56
Show Gist options
  • Save Joakineee/03c691a73517434cf53404ccc2f8bd1d to your computer and use it in GitHub Desktop.
Save Joakineee/03c691a73517434cf53404ccc2f8bd1d to your computer and use it in GitHub Desktop.
Pattern Matching exercise
-module(pattern_matching).
-export([xOr_1/2,xOr_2/2,xOr_3/2,xOr_4/2,test_xOr_1/0,test_xOr_2/0,test_xOr_3/0,test_xOr_4/0,test_maxThree/0,test_howManyEqual/0]).
xOr_1(X,Y) ->
X =/= Y.
xOr_2(X,Y) ->
(X and not Y) or (not X and Y).
xOr_3(X,Y) ->
(X or Y) and (not X or not Y).
xOr_4(X,Y) ->
X xor Y.
maxThree(X,Y,Z) ->
erlang:max(X,erlang:max(Y,Z)).
howManyEqual(X,X,X) -> 3;
howManyEqual(X,X,_) -> 2;
howManyEqual(X,_,X) -> 2;
howManyEqual(_,X,X) -> 2;
howManyEqual(_,_,_) -> 0.
%Tests functions begins here.
%Returns true if everything goes as expected.
test_xOr_1() ->
(xOr_1(true,true) == false) and
(xOr_1(true,false) == true).
test_xOr_2() ->
(xOr_2(true,true) == false) and
(xOr_2(true,false) == true).
test_xOr_3() ->
(xOr_3(true,true) == false) and
(xOr_3(true,false) == true).
test_xOr_4() ->
(xOr_4(true,true) == false) and
(xOr_4(true,false) == true).
test_maxThree() ->
maxThree(34,25,36) == 36.
test_howManyEqual() ->
(howManyEqual(34,25,36) == 0) and
(howManyEqual(34,25,34) == 2) and
(howManyEqual(34,34,34) == 3).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment