[erlang-questions] newbie needing help getting started

Jayson Vantuyl kagato@REDACTED
Wed Oct 14 08:07:31 CEST 2009


Short Version:

Compile the math1 module.  Either use this in the shell:

> c(math).


Or compile it first using the erlc command.

Or compile it using "erl -make" or "make:all()".  There are a few  
options.

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.

Long Version:

When you run commands in the shell, the system will automatically load  
compiled modules in the path.  Running:

> math1:factoral(0).

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
>   end,
>   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.

Good luck.

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
> http://erlang-consulting.com
>
> On 10 Oct 2009, at 14:57, Juan Backson <juanbackson@REDACTED> wrote:
>
>> Hi
>>
>> Thank you so much for your help.  I managed to compile the sample  
>> code, but
>> I can't run it:
>>
>> [root@REDACTED erlang]# cat math1.erl
>> -module(math1).
>> -export([factorial/1]).
>> factorial(0) -> 1;
>> factorial(N) -> N * factorial(N-1).
>>
>> [root@REDACTED erlang]# ls
>> math1.beam  math1.erl
>> [root@REDACTED 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?
>>
>> jb
>>
>> On Sat, Oct 10, 2009 at 9:51 PM, Joe Armstrong <erlang@REDACTED>  
>> wrote:
>>
>>> You cannot type -module(math1). directly into the shell.
>>> make a file (math.erl) containing the lines "-module(math1). ..."  
>>> etc.
>>> then compile the module in the shell like this:
>>>
>>>> c(math1).
>>>
>>> This will compile the code in math1.erl, then you can run the
>>> functions in the shell
>>>> math1:funcname(...) etc.
>>>
>>> /Joe
>>>
>>>
>>> On Sat, Oct 10, 2009 at 3:45 PM, Juan Backson  
>>> <juanbackson@REDACTED>
>>> wrote:
>>>> Hi,
>>>>
>>>> I am following the tutorial trying to get started.  I am getting  
>>>> error
>>> with
>>>> 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 ?
>>>>
>>>> jb
>>>>
>>>
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>



More information about the erlang-questions mailing list