Erlang – Basic Syntax ”; Previous Next In order to understand the basic syntax of Erlang, let’s first look at a simple Hello World program. Example Live Demo % hello world program -module(helloworld). -export([start/0]). start() -> io:fwrite(“Hello, world!n”). 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 like 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 delimited with the dot (.) symbol. Each statement in Erlang needs to end with this delimiter. An example from the Hello world program is as shown in the following program − io:fwrite(“Hello, world!n”). 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 divided into modules. A module consists of a sequence of attributes and function declarations. It is just like 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 functionality 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!n”). In the above code, we are using the import keyword to import the library ‘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 list of keywords in Erlang. after and andalso band begin bnot bor bsl bsr bxor case catch cond div end fun if let not of or orelse receive rem try when xor Comments in Erlang Comments are used to document your code. Single line comments are identified by using the % symbol at any position in the line. 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!n”). Print Page Previous Next Advertisements ”;
Category: erlang
Erlang – Modules
Erlang – Modules ”; Previous Next Modules are a bunch of functions regrouped in a single file, under a single name. Additionally, all functions in Erlang must be defined in modules. Most of the basic functionality like arithmetic, logic and Boolean operators are already available because the default modules are loaded when a program is run. Every other function defined in a module you will ever use needs to be called with the form Module:Function (Arguments). Defining a Module With a module, you can declare two kinds of things: functions and attributes. Attributes are metadata describing the module itself such as its name, the functions that should be visible to the outside world, the author of the code, and so on. This kind of metadata is useful because it gives hints to the compiler on how it should do its job, and also because it lets people retrieve useful information from compiled code without having to consult the source. The syntax of a function declaration is as follows − Syntax -module(modulename) Where, modulename is the name of the module. This has to be the first line of the code in the module. The following program shows an example of a module called helloworld. Example Live Demo -module(helloworld). -export([start/0]). start() -> io:fwrite(“Hello World”). The output of the above program is − Output Hello World Module Attributes A module attribute defines a certain property of a module. A module attribute consists of a tag and a value. The general syntax of an attribute is − Syntax -Tag(Value) An example of how the attribute can be used is shown in the following program − Example Live Demo -module(helloworld). -author(“TutorialPoint”). -version(“1.0”). -export([start/0]). start() -> io:fwrite(“Hello World”). The above program defines 2 custom attributes called author and version which contains the program author and program version number respectively. The output of the above program is − Output Hello World Pre-built Attributes Erlang has some pre-built attributes which can be attached to modules. Let’s take a look at them. Export The exports attribute will take a list of functions and arity to export for consumption by other modules. It will define the module interface. We have already seen this in all of our previous examples. Syntax export([FunctionName1/FunctionArity1,.,FunctionNameN/FunctionArityN]) Where, FunctionName − This is the name of the function in the program. FunctionArity − This is the number of parameters associated with the function. Example Live Demo -module(helloworld). -author(“TutorialPoint”). -version(“1.0”). -export([start/0]). start() -> io:fwrite(“Hello World”). The output of the above program will be − Output Hello World Import The import attribute is used to import functions from another module to use it as local. Syntax -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. Example Live Demo -module(helloworld). -import(io,[fwrite/1]). -export([start/0]). start() -> fwrite(“Hello, world!n”). In the above code, we are using the import keyword to import the library ‘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. The output of the above program will be − Output Hello, world! Print Page Previous Next Advertisements ”;
Erlang – Shell
Erlang – Shell ”; Previous Next The Erlang shell is used for testing of expressions. Hence, testing can be carried out in the shell very easily before it actually gets tested in the application itself. The following example showcases how the addition expression can be used in the shell. What needs to be noted here is that the expression needs to end with the dot (.) delimiter. After the command is executed, the shell prints out another prompt, this time for Command Number 2 (because the command number increases each time a new command is entered). The following functions are the most common one’s used in the Erlang shell. b() − Prints the current variable bindings. Syntax − b(). For example − Following is an example of how the function is used. First a variable called Str is defined, which has the value abcd. Then b() is used to display all the binded variables. f() − Removes all current variable bindings. Syntax − f(). For example − Following is an example of how the function is used. First a variable called Str is defined which has the value abcd. The f() is then used to remove the Str variable binding. The b() is then called to ensure the binding has been successfully removed. f(x) − Removes the binding for a particular variable. Syntax − f(x). Where, x – is the variable for which the binding needs to be removed. For example − Following is an example of how the function is used. First a variable called Str and Str1 are defined. The f(Str) is then used to remove the Str variable binding. The b() is then called to ensure the binding has been successfully removed. h() − Prints the history list of all the commands executed in the shell. Syntax − h(). For example − An example of the h() command, which prints the history of commands executed in the shell is shown in the following screenshot. history(N) − Sets the number of previous commands to keep in the history list to N. The previous number is returned. The default number is 20. Syntax − history(N). Where, N – is the number to which the command history list needs to be limited to. For example − An example of the history(N) command is shown in the following screenshot. e(N) − Repeats the command N, if N is positive. If it is negative, the Nth previous command is repeated (i.e., e(-1) repeats the previous command). Syntax − e(N). Where, N – is the command at the Nth position in the list. For example − An example of the e(N) command is shown below. Since we have executed the e(-1) command, it will execute the previous command which was history(5). Print Page Previous Next Advertisements ”;