Skip to content

Instantly share code, notes, and snippets.

@gorkaio
Created May 4, 2020 11:52
Show Gist options
  • Save gorkaio/c447de4a046af17689aa742318026923 to your computer and use it in GitHub Desktop.
Save gorkaio/c447de4a046af17689aa742318026923 to your computer and use it in GitHub Desktop.
-module(third).
-export([xOr_1/2, xOr_2/2, xOr_3/2, maxThree/3, howManyEqual/3]).
% XOR implementation #1
xOr_1(A,A) ->
false;
xOr_1(A,B) ->
A or B.
% XOR implementation #2
xOr_2(true,B) ->
not B;
xOr_2(_,B) ->
B.
% XOR implementation #3
xOr_3(A,B) ->
A =/= B.
% Max of three using pattern matching
maxThree(A,A,A) -> A;
maxThree(A,B,A) -> max(A,B);
maxThree(A,A,B) -> max(A,B);
maxThree(A,B,B) -> max(A,B);
% We could just use this, but we are supposed to pattern match here!
maxThree(A,B,C) ->
M=max(A,B),
max(M,C).
% How many equal using pattern matching
howManyEqual(A,A,A) -> 3;
howManyEqual(A,A,_) -> 2;
howManyEqual(_,A,A) -> 2;
howManyEqual(A,_,A) -> 2;
howManyEqual(_,_,_) -> 0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment