Created
March 20, 2016 03:16
-
-
Save mathiasverraes/765710b3e4eb4aba791d to your computer and use it in GitHub Desktop.
Bowling Kata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some tests, they are all True