How to code Fizzbuzz in F#

2019-03-12 02:39发布

I am currently learning F# and have tried (an extremely) simple example of FizzBuzz.

This is my initial attempt:

for x in 1..100 do 
    if x % 3 = 0 && x % 5 = 0 then printfn "FizzBuzz"  
    elif x % 3 = 0 then printfn "Fizz"
    elif x % 5 = 0 then printfn "Buzz"
    else printfn "%d" x

What solutions could be more elegant/simple/better (explaining why) using F# to solve this problem?

Note: The FizzBuzz problem is going through the numbers 1 to 100 and every multiple of 3 prints Fizz, every multiple of 5 prints Buzz, every multiple of both 3 AND 5 prints FizzBuzz. Otherwise, simple the number is displayed.

Thanks :)

标签: f# fizzbuzz
10条回答
Viruses.
2楼-- · 2019-03-12 02:53

Here's one more:

let fizzy num =     
   match num%3, num%5 with      
      | 0,0 -> "fizzbuzz"
      | 0,_ -> "fizz"
      | _,0 -> "buzz"
      | _,_ -> num.ToString()

[1..100]
  |> List.map fizzy
  |> List.iter (fun (s:string) -> printfn "%s" s)
查看更多
Evening l夕情丶
3楼-- · 2019-03-12 03:00

To add one more possible answer - here is another approach without pattern matching. It uses the fact that Fizz + Buzz = FizzBuzz, so you don't actually need to test for all three cases, you only need to see if it is divisible by 3 (then print "Fizz") and also see if it is divisible by 5 (then print "Buzz") and finally, print a new line:

for i in 1..100 do
  for divisor, str in [ (3, "Fizz"); (5; "Buzz") ] do
    if i % divisor = 0 then printf str
  printfn ""

The nested for loop assignes 3 and "Fizz" to divisor and str in the first iteration and then the second pair of values in the second iteration. The beneift is, you could easily add printing of "Jezz" when the value is divisible by 7 :-) ...in case that extensibility of the solution is a concern!

查看更多
太酷不给撩
4楼-- · 2019-03-12 03:01

I don't like all these repeated strings, here's mine:

open System
let ar = [| "Fizz"; "Buzz"; |]
[1..100] |> List.map (fun i ->
    match i % 3 = 0, i % 5 = 0 with
        | true, false ->  ar.[0]
        | false, true ->  ar.[1] 
        | true, true ->  ar |> String.Concat
        | _ -> string i
    |> printf "%s\n"
)
|> ignore
查看更多
▲ chillily
5楼-- · 2019-03-12 03:05

Here is my version:

//initialize array a with values from 1 to 100
let a = Array.init 100 (fun x -> x + 1)

//iterate over array and match *indexes* x
Array.iter (fun x ->
    match x with
        | _ when x % 15 = 0 -> printfn "FizzBuzz"
        | _ when x % 5 = 0 -> printfn "Buzz"
        | _ when x % 3 = 0 -> printfn "Fizz"
        | _ -> printfn "%d" x
) a

This is my first program in F#.

It's not perfect, but I think someone who starts learning F# (like me :)) can figure out what happens here quite fast.

However I am wondering what is the difference between matching to any _ or to x itself in pattern matching above?

查看更多
唯我独甜
6楼-- · 2019-03-12 03:06

I couldn't find a working solution that didn't include testing for i % 15 = 0. I've always felt that not testing for that is part of this "stupid" assignment. Be aware that this is probably not idiomatic F# since it's my first program in the language.

for n in 1..100 do 
  let s = seq { 
    if n % 3 = 0 then yield "Fizz"
    if n % 5 = 0 then yield "Buzz" } 
  if Seq.isEmpty s then printf "%d"n
  printfn "%s"(s |> String.concat "")
查看更多
甜甜的少女心
7楼-- · 2019-03-12 03:09

Yet one solution in F# style (i.e. with Active Patterns usage):

let (|P3|_|) i = if i % 3 = 0 then Some i else None
let (|P5|_|) i = if i % 5 = 0 then Some i else None

let f = function
  | P3 _ & P5 _ -> printfn "FizzBuzz"
  | P3 _        -> printfn "Fizz"
  | P5 _        -> printfn "Buzz"
  | x           -> printfn "%d" x

Seq.iter f {1..100}
//or
for i in 1..100 do f i
查看更多
登录 后发表回答