Quick Start

2 Executing Code

Once you've got LFE built, you want to play with it, right? Let's take a look at the REPL (interactive shell) first.

2.1 The REPL

To start the REPL, simply run the lfe script and tell erl (which is being run in the script) which additional code paths it should look for (in this case, the compiled LFE code):

$ ./bin/lfe -pa ./ebin

This assumes that you are still in the lfe directory where you build LFE. Running that command will dump you into the LFE REPL, looking something like this:

Erlang R15B03 (erts-5.9.3.1) [source] [64-bit] [smp:8:8] [async-threads:0] ...

LFE Shell V5.9.3.1 (abort with ^G)
>

Note that you can also start the the LFE shell manually from an existing Erlang shell (as we did earlier in this guide) or you can pass parameters to erl to start it up:

$ erl -pa ebin -s lfe_boot -noshell -noinput

Now try doing some basic operations:

> (+ 1 2 3)
6
> (cons 1 2)
(1 . 2)
> (cons (list 1 2) (list 3 4))
((1 2) 3 4)
>

Next, let's operate on some variables:

> (let ((x 123456789)) x)
123456789
> (let ((x 123456789)) (* x x))
15241578750190521
> (let ((x 123456789)) (* x x x x))
232305722798259244150093798251441
>

Looking good!

2.2 Running Scripts

Okay, so now that you can run things in the REPL, you want to run them as a script, yes? Here's how.

Let's create a temporary subdirectory to play in without fear of messing up our LFE directory:

$ mkdir tmp
$ cd tmp

Then, in that directory, let's create the following file and save it as hello.lfe:

(defmodule hello
  (export (start 0)))

(defun start ()
  (: io format '"Lfe says 'Hello, World!'~n"))

To compile that script and then run it, we can do this:

$ ../bin/lfec hello.lfe
$ ../bin/lfe -s hello start -s erlang halt

Or, if we want to use Erlang directly, we could do this:

$ erl -pa ../ebin -s lfe_comp file hello -s erlang halt
$ erl -pa ../ebin -s hello start -s erlang halt

Or, we could compile it and run it in the same command:

$ erl -pa ../ebin -s lfe_comp file hello -s hello start -s erlang halt

Note that this is the command line equivalent of the following:

$ erl -pa ../ebin
1> lfe_comp:file(hello).
{ok,hello}
2> hello:start().
Lfe says 'Hello, World!'
ok
3>

2.3 Running Scripts from the REPL

You can also use your new hello.lfe script in the REPL itself. There are two ways you can do this, using slurp or compiling the file. If you use slurp, all the functions are pulled into the shell namespace, and you won't have to reference the module name. Again, assuming that you are in lfe/tmp:

$ ../bin/lfe -pa ../ebin
> (slurp '"hello.lfe")
#(ok hello)
> (start)
Lfe says 'Hello, World!'
ok
>

If you choose to compile your module instead, you will use it like so:

$ ../bin/lfe -pa ../ebin
> (c '"hello")
#(module hello)
> (: hello start)
Lfe says 'Hello, World!'
ok
>

Note that in the second example, you need to reference the module.

For more information on the LFE shell, be sure to see the "REPL" section of the User Guide Introduction.