Skip to content

Instantly share code, notes, and snippets.

@mathiasverraes
Created March 20, 2016 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathiasverraes/765710b3e4eb4aba791d to your computer and use it in GitHub Desktop.
Save mathiasverraes/765710b3e4eb4aba791d to your computer and use it in GitHub Desktop.
Bowling Kata
module Bowling where
import Test.QuickCheck
import Data.List
data Frame =
Roll Int Int
| Spare Int Int
| SpareExtra Int
| Strike
| StrikeExtra Int Int
deriving (Show)
type Game = [Frame]
score :: Game -> Int
score [] = 0
score (Roll x y : rest) = x+y + (score rest)
score (Spare x y : rest) = x+y + (bonusForSpare rest) + (score rest)
score [SpareExtra _] = 0
score (Strike : rest) = 10 + (bonusForStrike rest) + (score rest)
score [StrikeExtra _ _] = 0
bonusForSpare [] = 0
bonusForSpare (Roll x _ : _) = x
bonusForSpare (Spare x _ : _) = x
bonusForSpare (SpareExtra x : _) = x
bonusForSpare (Strike : _) = 10
bonusForSpare _ = error "Inconceivable"
bonusForStrike [] = 0
bonusForStrike (Roll x y : _) = x + y
bonusForStrike (Spare x y : _) = x + y
bonusForStrike (Strike : []) = 10
bonusForStrike (Strike : Roll x _ : _) = 10 + x
bonusForStrike (Strike : Spare x _ : _) = 10 + x
bonusForStrike (Strike : Strike : _) = 10 + 10
bonusForStrike (Strike : StrikeExtra x _ : []) = 10 + x
bonusForStrike (StrikeExtra x y : []) = x + y
@mathiasverraes
Copy link
Author

Some tests, they are all True

let gutter = Roll 0 0 
let ones = Roll 1 1

0 == score []
0 == score [gutter]
0 == score (replicate 10 gutter)
20 == score (replicate 10 ones)
5 == score [(Roll 5 0)]
6 == score [(Roll 5 1)] 
10+5 + 5+5 == score [(Spare 9 1), (Roll 5 5)]
24 == score [Strike, (Roll 3 4)]
20 + 10 == score [Strike, Strike]
30 + 20 + 10 == score [Strike, Strike, Strike]
284 == score ((replicate 10 Strike) ++ [StrikeExtra 5 4])
300 == score ((replicate 10 Strike) ++ [StrikeExtra 10 10])

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