For those that don't know, there is a fully
Erlang Core-compatible
Lisp-2 that runs on the Erlang VM and produces
.beam files that can be used in any Erlang application. This manna from heaven is
LFE, or Lisp Flavored Erlang. It was started about 5 years ago by
Robert Virding, one of the co-creators of the Erlang programming language, and has inspired other similar efforts:
Elixir (a Ruby-alike) and
Joxa (a Lisp-1). (Incidentally, Robert has also created
Prolog and
Lua implementations that run on top of the Erlang VM!)
The new
LFE docs site (a continuous work in progress) has some good introductory materials for the curious reader:
This blog post aims to bring some of those hidden materials into the consciousness of Ubuntu users. If you are averse to Erlang syntax, LFE opens up a whole new world to you :-)
The examples below assume Ubuntu 12.10.
Getting Erlang
Erlang R15B01 comes with Ubuntu 12.10. If that's all you need, then this will suite you just fine:
$ sudo apt-get install erlang
If you are wanting to test against multiple versions of Erlang, you should check out the
kerl project, which lets you install a wide variety of Erlang versions (including the latest releases) side-by-side.
You'll also need git, if you don't yet have it installed:
$ sudo apt-get install git
Currently,
rebar is required to build all the LFE files. If you're going to be building LFE projects, you'll want this anyway ;-) Rebar will be in Ubuntu 13.04, but it's not in 12.10, so you'll need to get it:
$ wget https://github.com/rebar/rebar/wiki/rebar
$ chmod 755 rebar
$ sudo mv rebar /usr/local/bin
Getting and Building LFE
Here's what you need to do to build LFE:
$ mkdir -p ~/lab/erlang && cd ~/lab/erlang
$ git clone https://github.com/rvirding/lfe.git
$ cd lfe
$ make compile
If you looked at your
./ebin directory when you cloned the repo, you would have seen that there were no
.beam files in it. After compiling, it is full of
.beams ;-)
Sidebar: A common pattern in Erlang applications is the use of a
deps directory under one's project dir where dependencies can be installed without conflicting with any system-wide installs, providing versioning independence, etc. Managing these with rebar has been very effective for projects, where simply calling
rebar compile puts everything your app needs in
./deps. Projects that depend upon LFE are doing this, but we'll cover that in a future blog post.
Using LFE
With everything compiled, we can jump right in! Let's fire up the REPL, and do some arithmetic as a sanity check:
How about a message to
stdout?
Any form starting with
: is interpreted as a call to a module. The full form is
(: <module name> <function name> <arguments>). As such, you can see that we're calling the
format function in the (built-in)
io module.
Also, it's good to know that there are certain things that you can't do in the REPL, e.g., defining modules, macros, functions, and records. Erlang expects that these sorts of activities take place in modules. However, we can explore a little more before we write our first module. Let's use the REPL's
set form and
lambda to define a function anyway (albeit, in a somewhat awkward fashion):
That wasn't too bad ;-) We're seeing the external module call, again -- this time to the math library. Now let's use a module of our own devising...
Creating Modules
In another terminal (but same working directory), let's create a module in a file called my-module.lfe, with the following content:
Note that the module name in the code needs to match the file name (minus the extension) that you used for the module.
Back in the REPL terminal window, let's compile this module and run the defined function:
Let's add another function to the module that demonstrates the benefits of Erlang's multiple-arity support:
Re-compiling and running the functions, we are greeted with success:
Lastly, let's convert the power function we defined in the previous section using our REPL-workaround to a "real" function, defined in our new module:
And then let's try it out:
Perfect.
(Of course, it's rather absurd to redefine
pow to
exp, when there is basically nothing to gain by it ;-) It's just a quick demo...)
Conclusion
There's lots more to learn; this has been just a small sip from a hot mug o' LFE.
However, it's definitely enough to get you started and, should you be interested in following along in future LFE blog posts, you'll have everything you need to get the most out of those.