Created
October 15, 2017 14:51
-
-
Save ykon/ba765e7416f95a002bdb17e830c2cb77 to your computer and use it in GitHub Desktop.
FizzBuzz in F#
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
(* | |
* Copyright (c) 2017 Yuki Ono | |
* Licensed under the MIT License. | |
*) | |
open System | |
let commonFizzBuzz () = | |
printfn "commonFizzBuzz" | |
let mutable i = 1 | |
while i <= 100 do | |
if i % 15 = 0 then | |
printfn "FizzBuzz" | |
elif i % 3 = 0 then | |
printfn "Fizz" | |
elif i % 5 = 0 then | |
printfn "Buzz" | |
else | |
printfn "%d" i | |
i <- i + 1 | |
type FizzBuzzType = | |
| FizzBuzz of int | |
| Fizz of int | |
| Buzz of int | |
| Number of int | |
let toFizzBuzz = function | |
| n when n % 15 = 0 -> FizzBuzz(n) | |
| n when n % 3 = 0 -> Fizz(n) | |
| n when n % 5 = 0 -> Buzz(n) | |
| n -> Number(n) | |
let toFizzBuzzT n = | |
match n % 3, n % 5 with | |
| (0, 0) -> FizzBuzz(n) | |
| (0, _) -> Fizz(n) | |
| (_, 0) -> Buzz(n) | |
| _ -> Number(n) | |
let toStr = function | |
| FizzBuzz(_) -> "FizzBuzz" | |
| Fizz(_) -> "Fizz" | |
| Buzz(_) -> "Buzz" | |
| Number(n) -> string n | |
let funcFizzBuzz () = | |
printfn "funcFizzBuzz" | |
[1 .. 100] |> List.map toFizzBuzz |> List.map toStr |> List.iter (printfn "%s") | |
//[1 .. 100] |> List.map (toFizzBuzz >> toStr) |> List.iter(printfn "%s") | |
let seqFizzBuzz () = | |
printfn "seqFizzBuzz" | |
Seq.initInfinite toFizzBuzz |> Seq.take 100 |> Seq.map toStr |> Seq.iter (printfn "%s") | |
//Seq.initInfinite toFizzBuzz |> Seq.map toStr |> Seq.take 100 |> Seq.iter (printfn "%s") | |
//Seq.initInfinite (toFizzBuzz >> toStr) |> Seq.take 100 |> Seq.iter (printfn "%s") | |
[<EntryPoint>] | |
let main argv = | |
commonFizzBuzz () | |
//funcFizzBuzz () | |
//seqFizzBuzz () | |
Console.Read() |> ignore | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment