Packages

- Some miscellaneous functions in Elixir to start to get used to the syntax and functional programming style.

Current section

Files

Jump to
misc lib sum_list.ex
Raw

lib/sum_list.ex

defmodule SumList do
@moduledoc """
This module contains a function to sum any numbers in a list
"""
@doc """
This function will add a list of numbers together recursively
Examples:
iex> SumList.sum([1, 2, 3])
6
"""
def sum(list) do
_sum(list, 0)
end
# Private function to return early when the list is empty
defp _sum([], total) do
total
end
# Private function to sum the first number in the list
# with the current total, and sum the rest
# head - first item from the list
# tail - the rest of the list
# Example: [head|tail] = [1, [2, 3]]
defp _sum([head|tail], total) do
_sum(tail, head + total)
end
end