17.01.2015 Views

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

34<br />

the same atom for both. Do not abuse this power when you write your own functions – it<br />

should be done <strong>in</strong> a systematic way so that it is easy for your users to remember how to call<br />

your functions. If you create functions that differ only <strong>in</strong> arity but produce wildly different<br />

results, you will not be thanked for it. When <strong>in</strong> doubt, opt for giv<strong>in</strong>g the functions clearly<br />

different names.<br />

At any rate, always remember that <strong>in</strong> order to exactly specify which function we are<br />

talk<strong>in</strong>g about, we need to give the arity, not just the name.<br />

2.3.3 – Built-<strong>in</strong> functions <strong>and</strong> st<strong>and</strong>ard library modules<br />

Like any other programm<strong>in</strong>g language, <strong>Erlang</strong> comes with a st<strong>and</strong>ard library of useful<br />

functions. These are spread over a large number of modules; however, some st<strong>and</strong>ard<br />

library modules are more commonly used than others. In particular, the module named<br />

erlang conta<strong>in</strong>s those functions that are central to the entire <strong>Erlang</strong> system, that<br />

everyth<strong>in</strong>g else builds on. Another useful module that we have seen already is the lists<br />

module. The io module h<strong>and</strong>les basic text <strong>in</strong>put <strong>and</strong> output. The dict module provides<br />

hash-based associative arrays (dictionaries), <strong>and</strong> the array module provides extensible<br />

<strong>in</strong>teger-<strong>in</strong>dexed arrays. And so forth.<br />

Some functions are <strong>in</strong>volved with so low-level th<strong>in</strong>gs that they are really an <strong>in</strong>tr<strong>in</strong>sic part<br />

of the language <strong>and</strong> the run-time system. These are commonly referred to as built-<strong>in</strong><br />

functions, or BIFs, <strong>and</strong> like the <strong>Erlang</strong> run-time system itself, they are implemented <strong>in</strong> the C<br />

programm<strong>in</strong>g language. (Though some may disagree on the details, this is the most common<br />

def<strong>in</strong>ition.) In particular, all the functions <strong>in</strong> the erlang module are BIFs. Some BIFs, like<br />

our friend lists:reverse/1 from the previous section, could <strong>in</strong> pr<strong>in</strong>ciple be written directly <strong>in</strong><br />

<strong>Erlang</strong> (like most of the other functions <strong>in</strong> the lists module), but have been implemented<br />

<strong>in</strong> C for efficiency reasons. In general, though, the programmer does not have to care about<br />

how the functions are implemented—they look the same to the eye—but the term “BIF” is<br />

used quite often <strong>in</strong> the <strong>Erlang</strong> world, so it’s useful to know what it refers to.<br />

F<strong>in</strong>ally, even the operators of the language, such as +, are really built-<strong>in</strong> functions, <strong>and</strong><br />

belong to the erlang module. For example, you can write erlang:'+'(1,2) for addition.<br />

AUTOMATICALLY IMPORTED FUNCTIONS<br />

A few functions (all found <strong>in</strong> the module erlang) are both important <strong>and</strong> very commonly<br />

used, <strong>in</strong> <strong>Erlang</strong> programs as well as <strong>in</strong> the shell. These are automatically imported, which<br />

means that you don’t need to write the module name explicitly. We have already seen the<br />

function self(), which returns the process identifier (the “pid”) of the process that calls it.<br />

This is actually a remote call to erlang:self(), but because it is one of the auto-imported<br />

functions, you don’t need to prefix it with erlang:. Other examples are spawn(…), which<br />

starts a new process, <strong>and</strong> length(…), which computes the length of a list.<br />

2.3.4 – Creat<strong>in</strong>g modules<br />

To make ourselves a new module that can be used, we need to:<br />

• Write a source file<br />

©Mann<strong>in</strong>g Publications Co. Please post comments or corrections to the Author Onl<strong>in</strong>e forum:<br />

http://www.mann<strong>in</strong>g-s<strong>and</strong>box.com/forum.jspaforumID=454

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!