English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Erlang Basic Grammar

To understand the basic syntax of Erlang, let's first look at a simpleHello WorldProgram.

Example

% hello world program
-module(helloworld). 
-export([start/0]). 
start() -> 
   io:fwrite("Hello, world!\n").

For the above program, the following points should be noted:

  • The '%' symbol is used to add comments to the program.

  • The module statement is like adding a command space in any programming language. Here, it should be noted that this code will be part of a module named helloworld.

  • You can use the export function to use any function defined in the program. We are defining a function named start, and in order to use the start function, we must use the export statement./0 represents that our function 'start' accepts 0 parameters.

  • We finally defined the start function. Here we use another module named io, which has all the necessary input/output functions in Erlang. We use the fwrite function to output 'Hello World' to the console.

The output of the above program will be-

Output

Hello, world!

General form of declaration

In Erlang, you have already seen different symbols used in the Erlang language. Let's look at the content we see from a simple Hello World program-

  • Hyphen(–)It is usually used with module, import, and export statements. The hyphen is used to give each statement the corresponding meaning. Therefore, the example of the Hello world program is shown in the following program-

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

Each statement is terminated with a point(.)Symbol delimiter. Each statement in Erlang must end with this delimiter. An example of the Hello world program is shown below:

io:fwrite("Hello, world!\n").
  • Slash(/)Symbols are used with functions to define the number of parameters accepted by the function.

-export([start/0]).

Module

In Erlang, all code is divided into modules. Modules consist of a series of attribute and function declarations. Like the concept of namespace in other programming languages, this namespace is used to logically separate different code units.

Define module

Use module identifiers to define modules. The general syntax and examples are as follows.

Syntax

-module(ModuleName)

TheModuleNameThe requirement is the same file name minus the extension.erl. Otherwise, the code loading will not proceed as expected.

Example

-module(helloworld)

These modules will be introduced in subsequent chapters, just to give you a basic understanding of how to define a module.

Import declaration in Erlang

In Erlang, if you want to use the functions of an existing Erlang module, you can use the import statement. The following program describes the general form of the import statement-

Example

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

Where

  • Module name−This is the name of the module that needs to be imported.

  • Function name/Parameter −Functions that need to be imported in the module.

Let's change the way to write a hello world program using the import statement. The example will be displayed in the following program.

Example

% hello world program
-module(helloworld).
-import(io,[fwrite/1]).
-export([start/0]).
start() ->
   fwrite("Hello, world!\n").

In the above code, we use the import keyword to import the library 'io', especiallyfwritefunction. Therefore, whenever the fwrite function is called now, it is no longer necessary to mention it anywhereioModule name.

Keywords in Erlang

Keywords are reserved words in Erlang and must not be used for purposes other than the predefined ones. The following is a list of keywords in Erlang.

afterandandalsoband
beginbnotborbsl
bsrbxorcasecatch
conddivendfun
ifletnotof
ororelsereceiverem
trywhenxor

Erlang Comments

Comments are used to record code. Single-line comments are identified by using the % symbol at any position in the line. The following is the same as the-

Example

% hello world program
-module(helloworld).
% Import function to import the io module
-import(io,[fwrite/1]).
% Export function to ensure that the start function can be accessed.
-export([start/0]).
start() ->
   fwrite("Hello, world!\n").