Skip to content

Instantly share code, notes, and snippets.

@samjarman
Last active May 9, 2016 22:58
Show Gist options
  • Save samjarman/e76d1eb0bfa17834577712109dd994a3 to your computer and use it in GitHub Desktop.
Save samjarman/e76d1eb0bfa17834577712109dd994a3 to your computer and use it in GitHub Desktop.
Sums a lit of numbers in elixir
@doc """
iex>Misc.sum([1,1,1])
3
iex>Misc.sum([1, 2, 3, 4, 5])
15
"""
def sum(list) do
_sum(list, 0)
end
defp _sum([], total) do
total
end
defp _sum([head | tail], total) do
_sum(tail, head + total)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment