Skip to content

Instantly share code, notes, and snippets.

@pichi
Last active March 24, 2017 12:24
Show Gist options
  • Save pichi/1b8b11552266451da64daf4d720227a5 to your computer and use it in GitHub Desktop.
Save pichi/1b8b11552266451da64daf4d720227a5 to your computer and use it in GitHub Desktop.
Rock, paper, scissors game definition
-module(rps).
-export([beats/1]).
-export([result/2]).
-export([tournament/2]).
beats(rock) -> scissors;
beats(paper) -> rock;
beats(scissors) -> paper.
result(X, Y) ->
case {beats(X), beats(Y)} of
{Y, _} -> win;
{_, X} -> lose;
_ -> draw
end.
points(X, Y) ->
result2points(result(X, Y)).
result2points(win) -> 1;
result2points(draw) -> 0;
result2points(lose) -> -1.
tournament(L1, L2) ->
lists:sum(lists:zipwith(fun points/2, L1, L2)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment