Last active
April 25, 2016 19:49
-
-
Save fschuindt/d98ef12d8a3ae9c4b39037c94e54ba04 to your computer and use it in GitHub Desktop.
Sum of elements on a list. "The process of taking a list and reducing it down to one value is known as a reduce algorithm and is central to functional programming."
This file contains hidden or 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
defmodule Math do | |
def sum_list([head | tail], accumulator) do | |
sum_list(tail, head + accumulator) | |
end | |
def sum_list([], accumulator) do | |
accumulator | |
end | |
end | |
IO.puts Math.sum_list([1, 2, 3], 0) #=> 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment