1

As a learning Exercise with the FizzBuzz code in F#.

Reference with code here is working fine and looks good

In above code I want to learn/use match instead of if conditions as below so have wrote below function which works for any given int value.

let fizzbuzz num =
   match num with
   | x when (x % 3 = 0 && x % 5 = 0) -> printfn "%s" "FizzBuzz"
   | x when (x % 3 = 0) -> printfn "%s" "Fizz"
   | x when (x % 5 = 0) -> printfn "%s" "Buzz"
   | _ -> printfn "%s" (num.ToString())
;;

output:

fizzbuzz 15;;
FizzBuzz
val it : unit = ()

However In my above code needed to use list as input and make function as recursive. how can I do it? I understand working of list i.e using h::t and pass t as recurisve to fizzbuzz. I am struggling with syntax i guess.

Appericate you guidance on how to use list (pattern matching and accumulator argument) in above code?

Is there any blog/reference on how to use F# Interactive window with less keystokes as i found myself typing complete code again for any syntax error

cdlane
  • 40,441
  • 5
  • 32
  • 81
swapneel
  • 3,061
  • 1
  • 25
  • 32

3 Answers3

4

A bit more elegant solution than Daniel's, but same concept.

let fizzbuzz =
    List.iter (function
        | x when x % 3 = 0 && x % 5 = 0 -> printfn "FizzBuzz"
        | x when x % 3 = 0 -> printfn "Fizz"
        | x when x % 5 = 0 -> printfn "Buzz"
        | x -> printfn "%d" x)

And usage is same as with Daniel's solution:

[0..100] |> fizzbuzz

Edit: About the F# Interactive:

In general, I work with a script file and use Alt+' to execute code. This way I get syntax highlighting, design-time errors and IntelliSense.

When I do not have Visual Studio (and I don't like MonoDevelop, or whatever it is called), I use the command-line F# Interactive which has auto-complete (press TAB, it will auto-complete, press TAB again if you want different completion).

Edit again: Here is a solution with recursion and accumulator:

let fizzbuzz =
    let rec util acc = function
        | [] -> acc
        | h::t ->
            let h =
                match h with
                | x when x % 3 = 0 && x % 5 = 0 -> "FizzBuzz"
                | x when x % 3 = 0 -> "Fizz"
                | x when x % 5 = 0 -> "Buzz"
                | x -> string x
            util (acc + "\r\n" + h) t
    util "" >> fun x -> x.[ 2 .. ]

Example:

> fizzbuzz [1..15];;
val it : string =
  "1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz"
Ramon Snir
  • 7,520
  • 3
  • 43
  • 61
2

For me, the point of the fizzbuzz task is to keep the solution as simple as possible to get the job elegantly done. Simple code is generally more readable and easy to maintain. Your function uses match nicely to solve the core part and you just need to add iteration to call it for numbers from 1 to 100. This can be done using for loop:

for i in 1 .. 100 do
  fizzbuzz i

The task is imperative, because it requires you to print to the console, so adjusting it to a more functional version (i.e. collect the results as strings, instead of printing). This is a good thing to try next, because then you'll either need recursive list processing or functions like List.map (trying to solve the problem using both ways is a good way to learn).

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • there are so many ways to solve a simple task in f#. How do you decide which way is better? are there any suggested guidelines in functional language world? – swapneel Nov 07 '11 at 00:00
1
let rec fizzbuzz = function
  | [] -> ()
  | h :: t ->
    match h with
    | x when (x % 3 = 0 && x % 5 = 0) -> printfn "FizzBuzz"
    | x when (x % 3 = 0) -> printfn "Fizz"
    | x when (x % 5 = 0) -> printfn "Buzz"
    | x -> printfn "%d" x)
    fizzbuzz t

Usage:

[0..100] |> fizzbuzz

More options...

Community
  • 1
  • 1
Daniel
  • 47,404
  • 11
  • 101
  • 179