[erlang-questions] newbie needing help getting started
Wed Oct 14 08:07:31 CEST 2009
Compile the math1 module. Either use this in the shell:
Or compile it first using the erlc command.
Or compile it using "erl -make" or "make:all()". There are a few
In the shell and in Erlang .erl files, use periods, instead of
semicolons, to separate top-level definitions.
To see a complete, if empty, example of an Erlang build environment,
you can use git to checkout my skeleton deployment directory. It's a
work in progress, but it should be close.
When you run commands in the shell, the system will automatically load
compiled modules in the path. Running:
Will attempt to load math1.beam from the code path. To make this file
from a .erl file, you must compile it. Erlang provides a command-line
compiler (the erlc command), a shortcut to it within the shell (the
'c' command), and a full "make" system that can be used for more
complex, automatic builds.
There are a few different ways you can layout your code in
directories, but for starters, just put things in your current
directory, and it should find it.
Finally, a bit on syntax. In Erlang, there is a sort of weird syntax
based on punctuation. The way that I try to explain it:
* the dot (.): This is used to finish a definition.
* the comma (,): This is used to separate a series of actions that
occur in order.
* the semicolon (;): This is used to separate branches, where control
could go through one of the branches (but only one)
Consider the following:
> test(X) when is_atom(X) ->
> do_something(), % this is the first in a series of ordered
> instructions, use a comma
> do_something_else((), % another entry, with a comma
> case X of % The case is made up of different branches
> first_option -> something0(); % this is one branch
> second_option -> % this is another branch, it might run instead
> of the first one
> something1(), % this is a series of two instructions within
> the branch, note the comma
> something2(); % note the semicolon, which indicates the next
> line is another branch of the case
> third_option -> 5 % no punctuation here, as this is the end of
> the case
> something3(); % this semicolon indicates that there is another
> function head to match
> test(Y) when is_number(Y) ->
> 42. % note the period, this is the end of the statement, defining
> the function
Hopefully that's not too confusing, but getting the punctuation right
is important, if a bit difficult.
On Oct 10, 2009, at 12:14 PM, Roberto Aloi wrote:
> Just use a dot instead of a semicolon at the end of your commands.
> Roberto Aloi
> On 10 Oct 2009, at 14:57, Juan Backson <> wrote:
>> Thank you so much for your help. I managed to compile the sample
>> code, but
>> I can't run it:
>> [ erlang]# cat math1.erl
>> factorial(0) -> 1;
>> factorial(N) -> N * factorial(N-1).
>> [ erlang]# ls
>> math1.beam math1.erl
>> [ erlang]# erl
>> Erlang R13B02 (erts-5.7.3) [source] [64-bit] [smp:2:2] [rq:2]
>> [async-threads:0] [hipe] [kernel-poll:false]
>> Eshell V5.7.3 (abort with ^G)
>> 1> math1:factorial(0)
>> 1> ;
>> 1> math1:factorial(0);
>> What am I missing?
>> On Sat, Oct 10, 2009 at 9:51 PM, Joe Armstrong <>
>>> You cannot type -module(math1). directly into the shell.
>>> make a file (math.erl) containing the lines "-module(math1). ..."
>>> then compile the module in the shell like this:
>>> This will compile the code in math1.erl, then you can run the
>>> functions in the shell
>>>> math1:funcname(...) etc.
>>> On Sat, Oct 10, 2009 at 3:45 PM, Juan Backson
>>>> I am following the tutorial trying to get started. I am getting
>>>> the following sample code in ERL command line.
>>>> 10> -module(math1).
>>>> ** exception error: undefined shell command module/1
>>>> 11> -export([factorial/1]).
>>>> ** exception error: bad argument in an arithmetic expression
>>>> in operator '/'/2
>>>> called as factorial / 1
>>>> Could someone help me out ? What is wrong ?
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
More information about the erlang-questions