Last active
May 9, 2016 22:58
-
-
Save samjarman/e76d1eb0bfa17834577712109dd994a3 to your computer and use it in GitHub Desktop.
Sums a lit of numbers in elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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