DEV Community


Discussion on: Fizz Buzz in Every Language

bjorngrunde profile image
Björn Grunde

Here is another example using as a Module and using pattern matching. It looks hilarious :P

defmodule FizzBuzz do

  def run(num) when num >= 1 and num <= 100 do:
    fizzbuzz(num, rem(n, 3), rem(n, 5))
    run(num - 1)

  def run(0), do: {:ok, "Done"}
  def run(_) do: {:error, "Something went wrong"}

  defp fizzbuzz(_, 0, 0), do: IO.puts "Fizzbuzz"
  defp fizzbuzz(_, 0, _), do: IO.puts "Fizz"
  defp fizzbuzz(_, _, 0), do: IO.puts "Buzz"
  defp fizzbuzz(n, _, _), do: n |> to_string |> IO.puts

# Example