Call functions from shell using scripts

Hi there,

I have a script named ListModules.exs that contains the below,

defmodule ListModule do
  def sum([]), do: 0

  def sum([head | tail]) do
    head + sum(tail)
  end
end

How can I run the above from a windows shell and call the function sum([])?

I tried to run elixir -r ListModule.exs -e "ListModule.sum([])" and I don’t get back anything. If I try to parse the list [1,2,3] to the list argument it gives me the following error: "!VAR:" was unexpected at this time.

Any help is welcome.

Regards,

Try outputting the result, otherwise it is just thrown away:

➜  ~ elixir -r ListModule.exs -e "ListModule.sum([])"
➜  ~ elixir -r ListModule.exs -e "IO.inspect(ListModule.sum([]))"
0
➜  ~ elixir -r ListModule.exs -e "IO.inspect(ListModule.sum([1, 2, 3]))"
6
3 Likes

Thank you Nicd!