Created
January 13, 2018 11:44
-
-
Save elixirplayground/25ac7e177f109add25277f7a94cad2a6 to your computer and use it in GitHub Desktop.
elixirplayground.com code share
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
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