How to Read Program Arguments in Elixir

Elixir is fundamentally different than many of the programming languages most people use.

This also shows in the simple task of making the Hello World program that prints a hello to each argument to the program.

defmodule Test do

def hello([head|tail]) do
  IO.write("Hello #{head}\n")
  hello tail
end

def hello([]) do
end

end

Test.hello(System.argv)

Since Elixir has pattern matching, either the first or the second version of the hello function is called based on wether the input array has elements or not. If the first hello method is called it prints a hello to the first element, and calls “itself” with the rest of the elements.

Related Posts