Skip to content

Instantly share code, notes, and snippets.

@michaelfairley
Last active December 20, 2015 03:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelfairley/6063888 to your computer and use it in GitHub Desktop.
Save michaelfairley/6063888 to your computer and use it in GitHub Desktop.
Erlang bowling kata
-module(bowling).
-export([main/1]).
main(_) ->
test().
test() ->
% empty
[] = score([]),
% boring
[5, 11] = score([1, 4, 3, 3]),
% incomplete
[5] = score([1, 4, 3]),
% spare
[13, 19] = score([6, 4, 3, 3]),
% incomplete spare
[] = score([6, 4]),
% strike
[17, 24] = score([10, 4, 3]),
% incomplete strike
[] = score([10, 4]),
[] = score([10]),
% flubbed the end
[30, 60, 90, 120, 150, 180, 210, 240, 260, 270] = score([10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 0]),
% perfect
[30, 60, 90, 120, 150, 180, 210, 240, 270, 300] = score([10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]),
winner.
score(Rolls) ->
lists:sublist(score(Rolls, 0), 1, 10).
score([A,B,C|Rest], Previous) when A =:= 10 ->
[Previous+A+B+C | score([B,C|Rest], Previous+A+B+C)];
score([A,B,C|Rest], Previous) when A+B =:= 10 ->
[Previous+A+B+C | score([C|Rest], Previous+A+B+C)];
score([A,B|Rest], Previous) when A+B < 10 ->
[Previous+A+B | score(Rest, Previous+A+B)];
score(_, _) ->
[].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment