English 中文(简体)
Erlang - Basic Syntax
  • 时间:2024-09-08

Erlang - Basic Syntax


Previous Page Next Page  

In order to understand the basic syntax of Erlang, let’s first look at a simple Hello World program.

Example

% hello world program
-module(helloworld). 
-export([start/0]). 

start() -> 
   io:fwrite("Hello, world!
").

The following things need to be noted about the above program −

    The % sign is used to add comments to the program.

    The module statement is pke adding a namespace as in any programming language. So over here, we are mentioning that this code will part of a module called helloworld.

    The export function is used so that any function defined within the program can be used. We are defining a function called start and in order to use the start function, we have to use the export statement. The /0 means that our function ‘start’ accepts 0 parameters.

    We finally define our start function. Here we use another module called io which has all the required Input Output functions in Erlang. We used the fwrite function to output “Hello World” to the console.

The output of the above program will be −

Output

Hello, world!

General Form of a Statement

In Erlang, you have seen that there are different symbols used in Erlang language. Let’s go through what we have seen from a simple Hello World program −

    The hyphen symbol (–) is generally used along with the module, import and export statement. The hyphen symbol is used to give meaning to each statement accordingly. So examples from the Hello world program are shown in the following program −

-module(helloworld).
-export([start/0]).

Each statement is depmited with the dot (.) symbol. Each statement in Erlang needs to end with this depmiter. An example from the Hello world program is as shown in the following program −

io:fwrite("Hello, world!
").

    The slash (/) symbol is used along with the function to define the number of parameters which is accepted by the function.

-export([start/0]).

Modules

In Erlang, all the code is spanided into modules. A module consists of a sequence of attributes and function declarations. It is just pke a concept of a namespace in other programming languages which is used to logically separate different units of code.

Defining a module

A module is defined with the module identifier. The general syntax and example is as follows.

Syntax

-module(ModuleName)

The ModuleName needs to be same as the file name minus the extension .erl. Otherwise code loading will not work as intended.

Example

-module(helloworld)

These Modules will be covered in detail in the ensuing chapters, this was just to get you at a basic understanding of how a module should be defined.

Import Statement in Erlang

In Erlang, if one wants to use the functionapty of an existing Erlang module, one can use the import statement. The general form of the import statement is depicted in the following program −

Example

-import (modulename, [functionname/parameter]).

Where,

    Modulename − This is the name of the module which needs to be imported.

    functionname/parameter − The function in the module which needs to be imported.

Let’s change the way we write our hello world program to use an import statement. The example would be as shown in the following program.

Example

% hello world program
-module(helloworld).
-import(io,[fwrite/1]).
-export([start/0]).

start() ->
   fwrite("Hello, world!
").

In the above code, we are using the import keyword to import the pbrary ‘io’ and specifically the fwrite function. So now whenever we invoke the fwrite function, we don’t have to mention the io module name everywhere.

Keywords in Erlang

A Keyword is a reserved word in Erlang which should not be used for any different purposes other than the purpose which it has been intended for. Following are the pst of keywords in Erlang.

after and andalso band
begin bnot bor bsl
bsr bxor case catch
cond span end fun
if let not of
or orelse receive rem
try when xor

Comments in Erlang

Comments are used to document your code. Single pne comments are identified by using the % symbol at any position in the pne. Following is an example for the same −

Example

% hello world program
-module(helloworld).
% import function used to import the io module
-import(io,[fwrite/1]).
% export function used to ensure the start function can be accessed.
-export([start/0]).

start() ->
   fwrite("Hello, world!
").
Advertisements