Created
November 20, 2011 08:10
-
-
Save ritalin/1379965 to your computer and use it in GitHub Desktop.
Fizzbuzz By Computation Expression
This file contains 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
// 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