Recreating Elixir's Enum.sum
I’m learning Elixir, and today as an exercise, I was challenged to recreate Enum.sum using recursion. Here’s my solution. For those new to the language, Enum.sum iterates over a list and returns the sum. Here’s an example: iex(0)> Enum.sum([1,2,3,4,5]) 15 Here is my implementation: defmodule MyList do @moduledoc """ Defines the list struct and functions. """ @doc """ Add all the items in a list together. """ def sum(list) do sum(list, 0) end defp sum([], count) do count end defp sum([h|t], count) do sum(t, count + h) end end And the execution: ...