Skip to content

Instantly share code, notes, and snippets.

@elixirplayground
Created January 13, 2018 11:44
Show Gist options
  • Save elixirplayground/25ac7e177f109add25277f7a94cad2a6 to your computer and use it in GitHub Desktop.
Save elixirplayground/25ac7e177f109add25277f7a94cad2a6 to your computer and use it in GitHub Desktop.
elixirplayground.com code share
defmodule MyRecursion do
def sum(list, fun) do
do_sum(list, fun, 0)
end
defp do_sum([], _fun, acc) do
IO.puts acc
end
defp do_sum([h|t], fun, acc) do
operation = fun.(h)
result = operation + acc
do_sum(t, fun, result)
end
end
list = [1,2,3,4,5,6,7,8,9,10]
double = fn(x) -> x * 2 end
MyRecursion.sum(list, double)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment