31:72639365f60a
Anton Shestakov <engored@ya.ru>, Sun, 02 Mar 2014 22:41:13 +0900
Dedicated service: rps_nickserv.

next change 34:19dac34d9a51
previous change 24:19a7ba960f8e

src/rps3.erl

Permissions: -rw-r--r--

Other formats: Feeds:
-module(rps3).
-export([get_rules/0, winning_hand/1]).
get_rules() -> [rock, paper, scissors].
beats(H, [H|Tail]) ->
lists:last(Tail);
beats(H, [B,H|_]) ->
B;
beats(H, [_|T]) ->
beats(H, T).
loses(H) ->
beats(H, lists:reverse(get_rules())).
% winner for 3 hands is:
% rock, rock, rock -> rock
% rock, paper, scissors -> undefined
% rock, paper, paper -> paper
% rock, rock, paper -> paper
winning_hand(Hands) ->
HSet = sets:from_list(Hands),
sets:fold(
fun(Hand, Acc) ->
case sets:is_element(loses(Hand), HSet) of
true -> Acc;
false -> Hand
end
end, undefined, HSet).