Erlang (programming language)/Tutorials/Modules

From Citizendium
< Erlang (programming language)‎ | Tutorials
Revision as of 06:07, 8 August 2009 by imported>Tom Morris (Erlang programming language/Tutorials/Modules moved to Erlang (programming language)/Tutorials/Modules)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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.