Erlang (programming language)/Tutorials/Modules: Difference between revisions

From Citizendium
Jump to navigation Jump to search
imported>Eric Evers
(New page: =Erlang modules= Each Erlang_Programming source file utility.erl is required to be a separate module. Modules are created with the module statement. -module(utility). ...)
 
imported>Tom Morris
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{subpages}}
=Erlang modules=
=Erlang modules=


Line 8: Line 10:
  -module(utility).        % 1
  -module(utility).        % 1
  -export([rotate/0]).      % 2
  -export([rotate/0]).      % 2
-compile(export_all).
                           % 3
                           % 3
  rotate([H|T]) ->          % 4
  rotate([H|T]) ->          % 4
Line 23: Line 26:
"utility" is the module created by the file utility.erl
"utility" is the module created by the file utility.erl


utility functions like rotate can be imported else-where with:
utility functions like left rotate can be imported else-where with:
     -import(utility).
     -import(utility).


so now we do not need to use the "utility:" prefix.
so we do not need to use the "utility:" prefix.
Importing modules is not generally recommended. This is very similar to python and Java imports.
Importing modules is not generally recommended. This is very similar to python and Java imports.

Latest revision as of 06:07, 8 August 2009


Erlang modules

Each Erlang_Programming source file

utility.erl 

is required to be a separate module. Modules are created with the module statement.

-module(utility).         % 1
-export([rotate/0]).      % 2
-compile(export_all).
                          % 3
rotate([H|T]) ->          % 4
    T ++ [H].             % 5

compile with

    c(utility).

run with

    utility:rotate([1,2,3]).

and get

    [2,3,1].

"utility" is the module created by the file utility.erl

utility functions like left rotate can be imported else-where with:

    -import(utility).

so we do not need to use the "utility:" prefix. Importing modules is not generally recommended. This is very similar to python and Java imports.