DEV Community

Discussion on: Daily Challenge #88 - Recursive Ninjas

Collapse
 
thepeoplesbourgeois profile image
Josh • Edited

As always, there is no need to use any language but Elixir. We'll establish a signature with a guard clause to make sure the user can only pass a natural number, and a private chirp_ signature to do the work of building the list of chirps, with a default empty list as its second argument. We could use List.duplicate, if the point of this exercise wasn't to make a recursive call 😜

defmodule Ninja do
  def chirp(n) when is_integer(n) and n > 0, do: chirp_(n)

  defp chirp_(n, chirps \\ [])
  defp chirp_(0, chirps), do: Enum.join(chirps, "-")
  defp chirp_(n, chirps), do: chirp_(n-1, ["chirp" | chirps])
end