BIF(built-in functions) 顧名思義,就是Erlang內建函式。它們通常用來完成那些無法用Erlang完成的任務。比如將列錶轉換為元組或者獲取當前的時間和日期。完成這些操作的函式,我們稱之為BIF。
Built-in Functions
The following subsections will familiarize you with a few of the more commonly used
built-in functions grouped according to the type of function, with examples illustrating
their use. We will refer to built-in functions as BIFs, a practice almost universal in the
Erlang community. Standard and nonstandard BIFs are listed in the manual page of
the erlang module.
BIFs are usually written in C and integrated into the virtual machine (VM), and can be
used to manipulate, inspect, and retrieve data as well as interact with the operating
system. An example of a data manipulation function is the conversion of an atom to a
string: atom_to_list/1. Other BIFs, such as length/1, which returns the length of a list,
are implemented in the runtime system for efficiency.
Originally, all built-in functions were considered to belong to the module erlang, but
they have made their way to other modules for practicality and efficiency reasons.
Among the modules that contain built-in functions are ets and lists.
Although most built-in functions are seen as being an integral part of Erlang, others are
VM-dependent and do not necessarily exist in other VM implementations or even in
specific OS ports of the existing VM. Standard built-in functions are auto-imported, so
you can call them without the module prefix. Nonstandard BIFs, however, have to be
prefixed with the erlang module prefix, as in erlang:function. Examples of nonstan-
dard built-in functions include erlang:hash(Term, Range), which returns the hash of
the Term in the specified range; and erlang:display(Term), which prints the term to
standard output and is mainly used for debugging purposes.