재귀를 사용해 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
'호두나무 공방 > Exercism in Elixir' 카테고리의 다른 글
Collatz Conjecture - Exercism in Elixir (0) | 2022.03.29 |
---|---|
High Score - Exercism in Elixir (0) | 2021.12.01 |
Rational Numbers - Exercism in Elixir (0) | 2021.11.10 |
Two Fer - Exercism in Elixir (0) | 2021.11.10 |
High School Sweetheart - Exercism in Elixir (0) | 2021.11.09 |