Last active
February 21, 2017 01:48
-
-
Save 50kudos/09df656b961b9c1ef0892472219c0529 to your computer and use it in GitHub Desktop.
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(third). | |
-export([maxThree/3,howManyEqual/3,eXOR/2]). | |
eXOR(X,Y) when is_boolean(X) andalso is_boolean(Y) -> | |
case {X,Y} of | |
{Y,X} -> false; | |
_ -> true | |
end. | |
maxThree(A,B,C) when is_integer(A) andalso is_integer(B) andalso is_integer(C) -> | |
max(max(A,B),C). | |
howManyEqual(A,B,C) when is_integer(A) andalso is_integer(B) andalso is_integer(C) -> | |
case {A,B,C} of | |
{_,A,A} -> 3; | |
{B,A,_} -> 2; | |
{C,_,A} -> 2; | |
{_,C,B} -> 2; | |
{_,_,_} -> 0 | |
end. |
@blogscot oh, yes. Thank you for pointing out, awesome.
You can make howManyEqual without case:
howManyEqual(A, A, A) -> 3;
howManyEqual(_, A, A) -> 2;
howManyEqual(A, _, A) -> 2;
howManyEqual(A, A, _) -> 2;
howManyEqual(_, _, _) -> 0.
@hgisinger Thanks for sharing. I was aware of using functions matching but I felt guilty for defining the same 4+ functions' name.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One thing I notice about function howManyEqual is, the arguments A, B, C don't need to be integers. By taking the guard off this function it could be reused for all kinds of Erlang terms, e.g. tuples, lists, etc. It'll work just fine. :-)