Skip to content

Instantly share code, notes, and snippets.

@EarlOfBDE
Created May 8, 2020 19:00
Show Gist options
  • Save EarlOfBDE/33c9cdd0cd06e3b2f046896830cd9df1 to your computer and use it in GitHub Desktop.
Save EarlOfBDE/33c9cdd0cd06e3b2f046896830cd9df1 to your computer and use it in GitHub Desktop.
Functional Programming module 1.15
-module(variables_patterns).
-export([xORone/2,xORtwo/2,xORthree/2,maxThree/3,howManyEqual/3,testall/0]).
% @doc xORone performs an Exclusive OR using the inequality operator '=/='
xORone(X,Y) ->
X =/= Y.
% @doc xORtwo performs an Exclusive OR using a negated equality operator
xORtwo(X,Y) ->
not (X == Y).
% @doc xORthree performs an Exclusive OR using boolean logic
xORthree(X,Y) ->
% Note that parentheses are used to ensure proper order of operations
(not X and Y) or (X and not Y).
% @doc maxThree returns the maximum of three passed parameters
maxThree(A,B,C) ->
max(A,max(B,C)).
% @doc howManyEqual returns the quantity of the three passed parameters that are equal
% There is only one combination that has all three equal, whereas there are three where two can be equal
% This soution counts on the fact that the function will exit on the first successful evaluation
howManyEqual(A,A,A) -> 3;
howManyEqual(A,A,_) -> 2;
howManyEqual(A,_,A) -> 2;
howManyEqual(_,A,A) -> 2;
howManyEqual(_,_,_) -> 0.
% @doc testall evaluates all of the module functions, combining all results into one
testall() ->
not xORone(false,false) and xORone(false,true) and xORone(true,false) and not xORone(true,true) and
not xORtwo(false,false) and xORtwo(false,true) and xORtwo(true,false) and not xORtwo(true,true) and
not xORthree(false,false) and xORthree(false,true) and xORthree(true,false) and not xORthree(true,true) and
(maxThree(30,31,32)==32) and (maxThree(30,32,31)==32) and (maxThree(32,31,30)==32) and
(howManyEqual(1,1,1)==3) and
(howManyEqual(1,1,3)==2) and (howManyEqual(1,2,1)==2) and (howManyEqual(1,2,2)==2) and
(howManyEqual(1,2,3)==0).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment