Пробуя учебник эликсира, и я нашел эту очень интересную конструкцию для рекурсии.Эликсир, сопоставляющий головку в списке
Так у меня есть список:
flares = [
%{classification: :X, scale: 99, date: Date.from({1859, 8, 29})},
%{classification: :M, scale: 5.8, date: Date.from({2015, 1, 12})},
%{classification: :M, scale: 1.2, date: Date.from({2015, 2, 9})},
%{classification: :C, scale: 3.2, date: Date.from({2015, 4, 18})},
%{classification: :M, scale: 83.6, date: Date.from({2015, 6, 23})},
%{classification: :C, scale: 2.5, date: Date.from({2015, 7, 4})},
%{classification: :X, scale: 72, date: Date.from({2012, 7, 23})},
%{classification: :X, scale: 45, date: Date.from({2003, 11, 4})}
]
И я хочу, чтобы вычислить сумму scale
-х, но с допуском для каждой классификации. Я надеялся, что я мог бы сделать что-то вроде:
def total_flare_power(flares), do: total_flare_power(flares, 0)
def total_flare_power([%head{classification: :M} | tail], total) do
total_flare_power(tail, total + head.scale * 0.92)
end
def total_flare_power([%head{classification: :C} | tail], total) do
total_flare_power(tail, total + head.scale * 0.78)
end
def total_flare_power([%head{classification: :X} | tail], total) do
total_flare_power(tail, total + head.scale * 0.68)
end
def total_flare_power([], total), do: total
Но я endup с этим сообщением об ошибке:
** (FunctionClauseError) no function clause matching in Solar.total_flare_power/2
Похоже, мой им структуры, что я пытаюсь соответствовать голову на не работает.
Спасибо за кучу, я знал о карте, но поскольку я изучаю язык, я хочу сам реализовать этот материал. – Calin