Created
November 21, 2012 13:55
-
-
Save Ball/4124964 to your computer and use it in GitHub Desktop.
Type Driven 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
type Frame = StandardFrame of int * int | |
| SpareFrame of int * int | |
| StrikeFrame | |
type Game = Frame list | |
let score game = | |
let nextRoll frames = | |
match frames with | |
| [] -> 0 | |
| StandardFrame(a,_)::_ -> a | |
| SpareFrame(a,_)::_ -> a | |
| StrikeFrame::_ -> 10 | |
let secondRoll frames = | |
match frames with | |
| [] -> 0 | |
| StandardFrame(_,b)::t -> b | |
| SpareFrame(_,b)::t -> b | |
| StrikeFrame::t -> nextRoll t | |
let scoreFrame frame frames = | |
match frame with | |
| StandardFrame(a,b) -> a+b | |
| SpareFrame(a,b) -> a+b + nextRoll frames | |
| StrikeFrame -> 10 + (nextRoll frames) + (secondRoll frames) | |
let rec foldScore i score game = | |
match i,game with | |
| 0,_ -> score | |
| _,[] -> score | |
| _,StandardFrame(a,b) :: t -> foldScore (i-1) ((scoreFrame (StandardFrame(a,b)) t)+score) t | |
| _,SpareFrame(a,b) :: t -> foldScore (i-1) ((scoreFrame (SpareFrame(a,b)) t)+score) t | |
| _,StrikeFrame :: t -> foldScore (i-1) ((scoreFrame StrikeFrame t)+score) t | |
foldScore 10 0 game | |
score [StandardFrame(4,5); StandardFrame(0,0)] | |
score [SpareFrame(5,5); StandardFrame(2,3)] | |
[1..12] |> List.map (fun _ -> StrikeFrame) |> score |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment