5

Just trying to do simple sum of list values.

defmodule Mth do 

    def sum_list([]) do 
        0
    end

    def sum_list([H|T]) do
        H + sum_list(T)
    end

end

IO.puts Mth.sum_list([1, 2, 300]) 

But I get this error:

**(FunctionClauseError) no function clause matching in Mth.sum_list/1
    pokus.ex:3: Mth.sum_list([1, 2, 300])
    pokus.ex:14: (file)
    (elixir) src/elixir_lexical.erl:17: :elixir_lexical.run/2
    (elixir) lib/code.ex:316: Code.require_file/2**

5 Answers 5

18

You need to use lowercase letters for variable and functions names. Identifiers starting with uppercase are reserved for modules:

defmodule Mth do 

  def sum_list([]) do 
    0
  end

  def sum_list([h|t]) do
    h + sum_list(t)
  end

end

iex> IO.puts Mth.sum_list([1, 2, 300])
303
:ok
Sign up to request clarification or add additional context in comments.

1 Comment

It would be unwise to implement a sum_list function this way because you would not benefit from Elixir's tail call optimisation. h + sum_list(t) would be executed as follows 1. sum_list(t) 2. h + So the last function call here would be the +. This would mean that if you had a very long list you could get a stack overflow error. Somewhat ironic, I know.
9

To improve upon Chris's solution, if you want your sum function to be tail recursive, you'd need to modify it a bit to be:

defmodule Mth do 
  def sum_list(list), do: do_sum_list(list, 0)

  defp do_sum_list([], acc),    do: acc
  defp do_sum_list([h|t], acc), do: do_sum_list(t, acc + h)
end

iex> Mth.sum_list([1, 2, 300])
303

Comments

5

If you are going to use Enum.reduce you can go simpler:

defmodule Mth do
  def sum_list(list), do: Enum.reduce(list, 0, &(&1 + &2))
end

1 Comment

def sum_list(list), do: Enum.reduce(list, 0, &Kernel.+/2) this also works ;)
1

Just for sake of completeness this would also work:

defmodule Mth do

  def sum_list(list), do: do_sum_list(list,0)

  defp do_sum_list(l,i_acc), do: Enum.reduce(l, i_acc, fn(x, accum) -> x + accum end)

end

I post this solely for reference for others who may find this question and it's answers.

Comments

0

Try this:

sum_list=[1,2,300]     
Enum.sum sum_list     
303

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.