Hello World problem

At the “iex(1)>” prompt help says to use “cd/1” to change directory to where my program is but that gives an error. What am I missing?

What is it you are trying to achieve? Please do provide more context. As you can see, for me it works:

iex(1)> h cd

                               def cd(directory)

Changes the current working directory to the given path.

iex(2)> File.cwd
{:ok, "/home/nobbz"}
iex(3)> cd "/tmp"
/tmp
iex(4)> File.cwd
{:ok, "/tmp"}

Thanks - seems I did not know what “/1” meant and now I see it was cryptically explained in the help.

But now for the standard hello world:

defmodule m do
  def hello do
    io.puts "hello"
  end
end

I get:

== Compilation error on file elixirtut.ex ==
** (CompileError) elixirtut.ex:1: undefined function m/0
    (elixir) expanding macro: Kernel.defmodule/2
    elixirtut.ex:1: (file)

** (CompileError)  compile error
    (iex) lib/iex/helpers.ex:173: IEx.Helpers.c/2

It should be

defmodule M do
  def hello do
    IO.puts "hello"
  end
end
1 Like

Can you point to the location of the (official) docs where it is only cryptically explained? One of the declared goals of elixir is to have excellent documentation and I am sure action will be taken to make explanation of arity more clear if necessary!


There are two problems in this code.

  1. Modulenames need to be valid aliases or atoms. Atom named modules are for erlang libraries and shouldn’t be defined in elixir code, therefore you need to use an alias, which have to begin with a capital letter, e.g. M

  2. There is no variable io, therefore you will get an error because of that. Probably you want to remotely call into the module IO, therefore you have to write IO.puts "hello".

1 Like

Thanks - never had so much trouble getting a hello-world to run. I almost gave up. I wonder how many other novices did. I feel this is a bit indirect:

This message was triggered by invoking the helper h(),
usually referred to as h/0 (since it expects 0 arguments).

More direct might be “NOTE: numbers after the slash below are the number of arguments to be specified in parenthesis.” or something or better yet revamp the help like:

cd (filepath) … changes directory

I’m sure programmers that work with command oriented UI’s don’t have a problem but GUI guys like me might.

1 Like

BTW - it’s refreshing to see someone turn a question into an improvement - something I try to teach my guys to do with limited success.

1 Like

For a running start Erlang/Elixir Syntax: A Crash Course is worth a look at.

FYI: Modules

It contains a list of functions, each of which is written in the format <function name>/<arity>. Arity stands for the number of arguments.

Introduction to Mix is then typically the next step.

It is because we can have functions with the same name, but with different arity.

It is even possible to have functions, with the same name, the same arity, but working for different type of parameters (guard clause)

That is not often seen in other language.

It is valid to define

defmodule M do
  def hello do
    IO.puts "hello"
  end

  def hello(x) do
    IO.puts "hello #{x}"
  end

  def hello(x, y) do
    IO.puts "hello #{x} #{y}"
  end
end

Which message was triggered by h()?


And were did you find the confusing description of cd/1 you mentioned? Where did you find the confusing description of /x?

Typing “h()” lists help which includes the two lines:

This message was triggered by invoking the helper h(),
usually referred to as h/0 (since it expects 0 arguments).

I did not make the mental leap to generalize that help statement and apply it to the list of commands displayed.

Well, it is expected that one at least skims the guides or follow a book. Therefore it is not expected that one just fires up iex and enters h or h() blindly.

In the official guides the function name notion is explained on the second page, after playing around a bit in iex with very simple and basic arithmetic operators and string concatenation:

https://elixir-lang.org/getting-started/basic-types.html#identifying-functions

Identifying functions

Functions in Elixir are identified by both their name and their arity. The arity of a function describes the number of arguments which the function takes. From this point on we will use both the function name and its arity to describe functions throughout the documentation. round/1 identifies the function which is named round and takes 1 argument, whereas round/2 identifies a different (nonexistent) function with the same name but with an arity of 2.

The hereby explained and introduced way to write a function name is only used thereafter.

Also the explanation in the tool is as short as possible, since one who uses that tool would see it every time he uses it again and again, there that kind of documentation were annoying.

I think it is hard to make a good and exhaustive documentation inside of iex because of the given reasons, also I do not think it makes sense to push it to an earlier stage in the guides. One needs to know roughly what a function is and does before one can talk about how its name is written unambigously. It doesnt make sense the other way round.

Hah, I never mentally noticed that little bit before. ^.^

Cool. :slight_smile: