Skip to content

Instantly share code, notes, and snippets.

@fedeisas
Created February 20, 2017 22:01
Show Gist options
  • Save fedeisas/4679c4701d7541ba397024d812f16f0b to your computer and use it in GitHub Desktop.
Save fedeisas/4679c4701d7541ba397024d812f16f0b to your computer and use it in GitHub Desktop.
-module(maxThree).
-export([maxThree/3]).
maxThree(X, X, X) ->
X;
maxThree(X, X, Y) ->
case max(X, Y) of
X -> X;
Y -> Y
end;
maxThree(X, Y, X) ->
case max(X, Y) of
X -> X;
Y -> Y
end;
maxThree(Y, X, X) ->
case max(X, Y) of
X -> X;
Y -> Y
end;
maxThree(X, Y, Z) ->
case max(X, Y) of
X ->
case max(X, Z) of
X -> X;
Z -> Z
end;
Y ->
case max(Y, Z) of
Y -> Y;
Z -> Z
end
end.
@scull7
Copy link

scull7 commented Feb 21, 2017

Are the case statements necessary? These seemed to work, though they might fail for strange values.

maxThree(X,X,X) ->                                                                                                               
        X;                                                                                                                       
maxThree(X,X,Y) ->                                                                                                               
        max(X,Y);                                                                                                                
maxThree(X,Y,Y) ->                                                                                                               
        max(X,Y);                                                                                                                
maxThree(X,Y,Z) ->                                                                                                               
        max(max(X,Y),Z). 

@scull7
Copy link

scull7 commented Feb 21, 2017

Seems to work with order capable values:

83> one_fifteen:maxThree(a,b,c).       
c
84> one_fifteen:maxThree(c,a,b).
c
85> one_fifteen:maxThree("foo","bar","baz").
"foo"

@foxbunny
Copy link

@scull7, the first 3 patterns are not necessary as they are also covered by the last one.

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