호두나무 공방/Exercism in Elixir

Bird Count - Exercism in Elixir

2021. 11. 21. 20:02

문제 보기

재귀를 사용해 count, sum 등의 동작을 직접 구현해야 하는 문제였다. 가끔 이렇게 야크 털을 직접 깎는 것도 또 재미다.

defmodule BirdCount do
  def today([]), do: nil
  def today([h | _t]), do: h

  def increment_day_count([]), do: [1]
  def increment_day_count([h | t]), do: [h + 1 | t]

  def has_day_without_birds?([]), do: false
  def has_day_without_birds?([h | _t]) when h == 0, do: true
  def has_day_without_birds?([_h | t]), do: has_day_without_birds?(t)

  def total([]), do: 0
  def total([h | t]), do: h + total(t)

  def busy_days(list), do: do_busy_days(list, 0)

  defp do_busy_days([], days), do: days
  defp do_busy_days([h | t], days) when h >= 5, do: do_busy_days(t, days + 1)
  defp do_busy_days([_h | t], days), do: do_busy_days(t, days)
end