воскресенье, 7 февраля 2016 г.

Snooker “get remaining points” small Erlang thing

Just a little reminder for myself on Erlang module structure and common mistakes I’ve made composing this. It is a little Erlang module calculating how many points are on the snooker table. Number of the red balls in represented as integer, color balls are atoms.

-module(snooker).

-export([get_points/1]).

get_points(NumberOfReds) when is_integer(NumberOfReds) -> get_points(NumberOfReds, 0);
get_points(0) -> get_points(yellow, 0);
get_points(yellow) -> get_points(yellow, 0);
get_points(green) -> get_points(green, 0);
get_points(brown) -> get_points(brown, 0);
get_points(blue) -> get_points(blue, 0);
get_points(pink) -> get_points(pink, 0);
get_points(black) -> get_points(black, 0).

get_points(NumberOfReds, Total) when is_integer(NumberOfReds) andalso NumberOfReds =/= 0 -> get_points(NumberOfReds - 1, Total) + 1 + get_points(black);
get_points(0, Total) -> get_points(yellow, Total);
get_points(yellow, Total) -> get_points(green, Total) + 2;
get_points(green, Total) -> get_points(brown, Total) + 3;
get_points(brown, Total) -> get_points(blue, Total) + 4;
get_points(blue, Total) -> get_points(pink, Total) + 5;
get_points(pink, Total) -> get_points(black, Total) + 6;
get_points(black, Total) -> Total + 7.

Комментариев нет:

Отправить комментарий