2 min read

an LFE program is composed of anywhere from 1 to hundreds of thousands of lightweight processes. each process operates independently with its own private memory, communicating via message passing, and the overhead for an Erlang process is pretty low. Processes in LFE are built from functions. These running functions communicate with other running functions via messages. When you start up the LFE REPL, you're using an Erlang process, and you can communicate with it just like any other process.
>(set pid (self))

The messages are sitting in the inbox of the process they were sent to, the REPL. If we flush the REPL's inbox, we can see them:
>(: c flush)
Let's send another message to the REPL's message queue (inbox):
>(! pid (list 3 2 1))
Now let's take a look at that message without flushing the queue:

((list a b c) 
(: io format '"results: ~p ~p ~p~n" (list a b c))))

If there is a message in the inbox matching the pattern we have defined (in this case, a list of length 3), then we will have access to the data that is matched and bound to the variables.

If there are a bunch of messages in the inbox, they will all be iterated over until a match is found:

>(! pid (tuple 3 2 1))
>(! pid (tuple 2 1))
>(! pid (tuple 1))

 ((tuple a)   
 (: io format '"results: ~p ~n" (list a))))

we've only look at the process for the REPL itself. We'd like to expand our horizons and look at creating a process in the REPL, writing to it instead of our shell.

However, we are faced with a difficulty: Erlang's spawn function takes a module and function as a parameter.

Here is a simple function called print-result that responds to messages when it's running as an Erlang process using a call to receive:

>(defun print-result () (receive

(: io format '"lfe message: '~s'~n" (list msg))))))

Now that we've got our message-capable function, let's spawn it and capture the process id so that we can write to it:
> (set pid (spawn (lambda () (print-result))))

>(! pid '"I am learning List Flavored Erlang.") output
"I am learning List Flavored Erlang." lfe message: 'I am learning List Flavored Erlang.'

Code tested on Ubuntu 20.04