Quick Start

3 Using Libraries

3.1 OTP Modules

Taking advantage of the Erlang stdlib is straightforward. All you need to do is prepend the call with a : and adjust to use the LFE syntax vs. the Erlang syntax.

In fact, we've already seen an example of this in Section 2 when we called (: io format ...) as part of a "Hello World."

Here's an example base64 usage from the Erlang stdlib:

> (: base64 encode_to_string '"Space is big. Really big.")
"U3BhY2UgaXMgYmlnLiBSZWFsbHkgYmlnLg=="
> (: base64 decode_to_string '"QW5kIHNvIHRoZSBVbml2ZXJzZSBlbmRlZC4=")
"And so the Universe ended."

3.2 Processes in LFE

One of the first things that people want to do with LFE is examine the message passing syntax so that they can compare it with vanilla Erlang. Here's a small example of what this looks like in LFE:

(defun print-result ()
  (receive
    (msg
      (: io format '"Received message: '~s'~n" (list msg))
      (print-result))))

If that was saved in a module called messenger, then one could utilize it thusly:

> (set pid (spawn 'messenger 'print-result ()))
<0.34.0>
> (! pid '"Ford is missing.")
"Ford is missing."
Received message: 'Ford is missing.'

For more information on working with processes in LFE, be sure to view the tutorial.

Related to this, you can find details and discussion around OTP and creating your own servers in the OTP Servers tutorial.

3.3 Third-Party Libraries

Finally, accessing code that is written in third-party libraries is exactly the same. Simply use the modules they have provided. If you started the LFE REPL pointing to your third-party libraries with a -pa (path) option, then all you have to do is this:

> (: your-module some-function '"some parameter")

That's it!