Erlang - BIFS


Advertisements

BIFs are functions that are built into Erlang. They usually do tasks that are impossible to program in Erlang. For example, it’s impossible to turn a list into a tuple or to find the current time and date. To perform such an operation, we call a BIF.

Let’s take an example of how BIF’s are used −

Example

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

start() ->   
   io:fwrite("~p~n",[tuple_to_list({1,2,3})]), 
   io:fwrite("~p~n",[time()]).

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

  • In the first example, we are using the BIF called tuple_to_list to convert a tuple to a list.

  • In the second BIF function, we are using the time function to output the system time.

The output of the above program will be as follows −

Output

[1,2,3]
{10,54,56}

Let’s look at some of the more BIF functions available in Erlang.

Sr.No. BIF Functions & Description
1

date

This method returns the current system date.

2

byte_size

This method returns the number of bytes contained in a Bitstring.

3

element

The method returns the Nth element in the tuple.

4

float

This method returns the float value of a particular number.

5

get

The method returns the process dictionary as a list.

6

put

This method is used to put a key,value pair in the process dictionary.

7

localtime

The method is used to give the local date and time in the system.

8

memory

Returns a list containing information about memory dynamically allocated by the Erlang emulator.

9

now

This method returns the tuple {MegaSecs, Secs, MicroSecs} which is the elapsed time since 00:00 GMT, January 1, 1970.

10

ports

Returns a list of all ports on the local node

11

processes

Returns a list of process identifiers corresponding to all the processes currently existing on the local node.

12

universaltime

Returns the current date and time according to Universal Time Coordinated (UTC).

Advertisements