Skip to content

Instantly share code, notes, and snippets.

@50kudos
Last active February 21, 2017 01:48
Show Gist options
  • Save 50kudos/09df656b961b9c1ef0892472219c0529 to your computer and use it in GitHub Desktop.
Save 50kudos/09df656b961b9c1ef0892472219c0529 to your computer and use it in GitHub Desktop.
-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
Copy link

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. :-)

@50kudos
Copy link
Author

50kudos commented Feb 20, 2017

@blogscot oh, yes. Thank you for pointing out, awesome.

@hgisinger
Copy link

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.

@50kudos
Copy link
Author

50kudos commented Feb 21, 2017

@hgisinger Thanks for sharing. I was aware of using functions matching but I felt guilty for defining the same 4+ functions' name. :octocat:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment