Skip to content

Instantly share code, notes, and snippets.

@fschuindt
Last active April 25, 2016 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fschuindt/d98ef12d8a3ae9c4b39037c94e54ba04 to your computer and use it in GitHub Desktop.
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."
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