One problem, five solutions — fizz buzz

Kevin Da Silva
4 min readAug 4, 2022

Hello reader, today heavily inspired by Conor Hoekstra (code_report) on YouTube I brought a computer science problem to compare between multiple programming languages, the languages we are going to compare the pros and cons are python, JavaScript, Haskell, elixir, and vlang.

But first, let’s have a context of the problem:

Given a positive integer A, return an array of strings with all the integers from 1 to N.

But for multiples of 3 the array should have “Fizz” instead of the number.

For the multiples of 5, the array should have “Buzz” instead of the number.

For numbers which are multiple of 3 and 5 both, the array should have “FizzBuzz” instead of the number.

Now that we have the context of the problem, we are ready to start comparing our first solution, the python solution:

def start(amount): 
input = list(range(1, amount+1))
return map(fizz_buzz, input)
def fizz_buzz(num):
final = “” if(num%3 == 0):
final += “Fizz”
if(num%5 == 0):
final += “Buzz”
if(final == “”):
return num
return final

I'm not an expert in python, but I really like how the code was structured in this solution, it's super interesting that python has a range function to generate the input, but I have to say that the only point I dislike about this solution is that we had to hurt immutability by reassigning the value of the final variable multiple times, I know that an immutable solution is possible in python but it, in general, doesn't feel organic in c like programming languages

The second solution is also a c like language, it's the JavaScript version:

const start = amount => {  
const output = []
for (let index = 1; index < amount+1; index++) {
output.push(fizzBuzz(index))
}
return output
}
const fizzBuzz = num => {
let final = ""
if(num%3 == 0)
final += "Fizz"
if(num%5 == 0)
final += "Buzz"
if(final == "")
return num
return final
}

The resolution logic is very similar to the python version. Still, unfortunately, JavaScript doesn't have a range function, making the solution more verbose and less immutable due to having to push an array for the output and due to using a loop that changes state, fizz buzz definitively doesn't make JS shine.

The third solution is the elixir solution:

defmodule FizzBuzz do    
def start(amount), do: 1..amount |> Enum.map(&fizz_buzz(&1))
def fizz_buzz(num) do
dividedByThree = rem(num, 3) == 0
dividedByFive = rem(num, 5) == 0
cond do
dividedByThree && dividedByFive -> "FizzBuzz"
dividedByThree -> "Fizz"
dividedByFive -> "Buzz"
true -> num
end
end
end

Besides having to create a module to be able to structure the solution, I really like elixir and how immutability felt natural while expressed in this solution making it our first immutable solution, I also like how the start function turns out to be just a one-liner due to piping computations.

Now comes our second immutable solution, the Haskell one:

start amount = map fizzBuzz [1..amount] fizzBuzz num  
| dividedByThree && dividedByFive = "FizzBuzz"
| dividedByThree = "Fizz"
| dividedByFive = "Buzz"
| otherwise = show num
where
dividedByThree = num `rem` 3 == 0
dividedByFive = num `rem` 5 == 0

Haskell is my favorite programming language and simply love how programs are expressed in such a simple and expressive way, and this solution also highlights a one-liner function start and a fizzBuzz function that just uses a guard operator to turn the solution complete.

And our last solution is also an immutable solution, and it was implemented in a brand-new programming language the vlang, a programming language very close to GO and with a lot of potential:

fn start(amount int) []string { 
input := []int{len: amount, init: it+1}
return input.map(fizz_buzz)
}
fn fizz_buzz(num int) string {
return match true {
num%3 == 0 && num%5 == 0 { 'FizzBuzz' }
num%3 == 0 { 'Fizz' }
num%5 == 0 { 'Buzz' } else { '$num' }
}
}

The v solution looked pretty interesting and satisfactory to me because v is immutable by default, so I was able to express immutable solutions in a way that respects the design of the language which seemed very interesting to me, v is definitively a language I hope to thrive in the future.

With all that said and with all the solutions shown, I think we got to the end of one more article, so if you want to give your contribution to an improvement of any solutions or provide solutions for new programming languages, feel free to put them in the comments section.

Also, if you want to see a video version of this article I just created an YouTube channel and the first video was me comparing the same solutions, to watch the video just click here!

To check the gists of the solutions, click here!

Thank you for reading, See ya!

--

--

Kevin Da Silva

I'm a back-end developer, functional programming lover, fascinated by computer science and languages. From the south part of Brazil to the world