Skip to content

Instantly share code, notes, and snippets.

@ritalin
Created November 20, 2011 08:10
Show Gist options
  • Save ritalin/1379965 to your computer and use it in GitHub Desktop.
Save ritalin/1379965 to your computer and use it in GitHub Desktop.
Fizzbuzz By Computation Expression
// builder
let fb_return = fun v ->
match v with
| (x, "") -> x.ToString()
| (_, s) -> s
type FizzBuzzBuilder() =
member this.Return v = fb_return v
let fizzbuzz = FizzBuzzBuilder()
// predicator
let fizz = fun x ->
match x with
| x when x % 3 = 0 -> "Fizz"
| _ -> ""
let buzz = fun x ->
match x with
| x when x % 5 = 0 -> "Buzz"
| _ -> ""
// test
[1..30]
|> List.map(fun x -> fizzbuzz {
return (x, fizz(x) + buzz(x))
})
|> Seq.iter(fun x -> printfn "%s" x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment