Torounia

Torounia

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,

Marked As Solved

Nicd

Nicd

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

Last Post!

Torounia

Torounia

Thank you Nicd!

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement