Skip to content

Instantly share code, notes, and snippets.

@i-e-b
Created May 4, 2012 16:45
Show Gist options
  • Save i-e-b/2596139 to your computer and use it in GitHub Desktop.
Save i-e-b/2596139 to your computer and use it in GitHub Desktop.
A unit-tested fizz-buzz in F#
module program
let fizzbuzz n =
let (|DivisibleBy|_|) x i = if i%x=0 then Some i else None
match n with
| DivisibleBy 3 _ & DivisibleBy 5 _ -> "FizzBuzz"
| DivisibleBy 3 _ -> "Fizz"
| DivisibleBy 5 _ -> "Buzz"
| x -> string x
let rec ``Write FizzBuzz for these numbers:`` = function
| head :: tail -> printfn "%s" (fizzbuzz head); ``Write FizzBuzz for these numbers:`` tail
| [] -> printfn ""
``Write FizzBuzz for these numbers:`` [1..100]
(********* Sample unit tests *************)
open FsUnit
[<Scenario("We're playing Fizz-Buzz")>]
type ``Given a game of fizz-buzz`` ()=
[<Then>] member
check.``when I play fizz-buzz on a number that is prime, I should get that number as a string`` ()=
(fizzbuzz 7) --> should equal "7"
[<Then>] member
check.``when I play fizz-buzz on a number that is divisible by 5 but not 3, I should get "Buzz"`` ()=
(fizzbuzz 5) --> should equal "Buzz"
[<Then>] member
check.``when I play fizz-buzz on a number that is divisible by 3 but not 5, I should get "Fizz"`` ()=
(fizzbuzz 3) --> should equal "Fizz"
[<Then>] member
check.``when I play fizz-buzz on a number that is divisible by both 3 and 5, I should get "FizzBuzz"`` ()=
(fizzbuzz 15) --> should equal "FizzBuzz"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment