LFE (programming language)

{{Primary sources|date=August 2020}}

{{Infobox programming language

| name = LFE

| logo = LFE (Lisp Flavored Erlang) Logo.png

| paradigm = Multi-paradigm: concurrent, functional

| family = Erlang, Lisp

| designer = Robert Virding

| developer = Robert Virding

| released = {{Start date and age|2008|df=yes}}

| latest release version = 2.1.1

| latest release date = {{Start date and age|2023|01|06|df=yes}}

| typing = dynamic, strong

| scope =

| programming language = Erlang

| operating system = Cross-platform

| license = Apache 2.0

| file ext = .lfe .hrl

| file format =

| website = {{URL|lfe.io}}

| implementations =

| dialects =

| influenced by = Erlang, Common Lisp, Maclisp, Scheme, Elixir, Clojure, Hy

| influenced = Joxa, Concurrent Schemer

}}

Lisp Flavored Erlang (LFE) is a functional, concurrent, garbage collected, general-purpose programming language and Lisp dialect built on Core Erlang and the Erlang virtual machine (BEAM). LFE builds on Erlang to provide a Lisp syntax for writing distributed, fault-tolerant, soft real-time, non-stop applications. LFE also extends Erlang to support metaprogramming with Lisp macros and an improved developer experience with a feature-rich read–eval–print loop (REPL).{{cite web |url=http://www.erlang-factory.com/upload/presentations/61/Robertvirding-LispFlavouredErlang.pdf |title=Lisp Flavored Erlang |last=Virding |first=Robert |website=Erlang Factory |access-date=2014-01-17}} LFE is actively supported on all recent releases of Erlang; the oldest version of Erlang supported is R14.

History

= Initial release =

Initial work on LFE began in 2007, when Robert Virding started creating a prototype of Lisp running on Erlang.{{cite web |url=https://groups.google.com/d/msg/lisp-flavoured-erlang/XA5HeLbQQDk/TUHabZCHXB0J |title=LFE History on the Lisp Flavored Erlang mail list |access-date=2014-05-28}} This work was focused primarily on parsing and exploring what an implementation might look like. No version control system was being used at the time, so tracking exact initial dates is somewhat problematic.

Virding announced the first release of LFE on the Erlang Questions mail list in March 2008.{{cite web |url=https://groups.google.com/d/msg/erlang-questions/rf43UofUvvQ/2-L1AecnkUoJ |title=LFE announcement on Erlang Questions mail list |access-date=2014-01-17}} This release of LFE was very limited: it did not handle recursive letrecs, binarys, receive, or try; it also did not support a Lisp shell.

Initial development of LFE was done with version R12B-0 of Erlang{{cite web |url=https://groups.google.com/d/msg/erlang-questions/rf43UofUvvQ/dthHortHwGoJ |title=Follow-up to LFE announcement on Erlang Questions mail list |access-date=2014-01-17}} on a Dell XPS laptop.{{cite interview |last1=Armstrong |first1=Joe |last2=Virding |first2=Robert |url=http://www.erlang.org/course/history.html |interviewer=Duncan McGreggor |title=Hardware used in the development of Erlang and LFE |date=2013-12-30 |type=Email exchange |access-date=2014-01-17}}

{{Lisp}}

= Motives =

Robert Virding has stated that there were several reasons why he started the LFE programming language:

  • He had prior experience programming in Lisp.
  • Given his prior experience, he was interested in implementing his own Lisp.
  • In particular, he wanted to implement a Lisp in Erlang: not only was he curious to see how it would run on and integrate with Erlang, he wanted to see what it would look like.
  • Since helping to create the Erlang programming language, he had had the goal of making a Lisp which was specifically designed to run on the BEAM and able to fully interact with Erlang/OTP.
  • He wanted to experiment with compiling another language on Erlang. As such, he saw LFE as a means to explore this by generating Core Erlang and plugging it into the backend of the Erlang compiler.

Features

Syntax and semantics

= Symbolic expressions (S-expressions) =

Like Lisp, LFE is an expression-oriented language. Unlike non-homoiconic programming languages, Lisps make no or little syntactic distinction between expressions and statements: all code and data are written as expressions. LFE brought homoiconicity to the Erlang VM.

= Lists =

In LFE, the list data type is written with its elements separated by whitespace, and surrounded by parentheses. For example, {{Lisp2|(list 1 2 'foo)}} is a list whose elements are the integers {{Lisp2|1}} and {{Lisp2|2}}, and the atom foo. These values are implicitly typed: they are respectively two integers and a Lisp-specific data type called a symbolic atom, and need not be declared as such.

As seen in the example above, LFE expressions are written as lists, using prefix notation. The first element in the list is the name of a form, i.e., a function, operator, or macro. The remainder of the list are the arguments.

= Operators =

The LFE-Erlang operators are used in the same way. The expression

(* (+ 1 2 3 4 5 6) 2)

evaluates to 42. Unlike functions in Erlang and LFE, arithmetic operators in Lisp are variadic (or n-ary), able to take any number of arguments.

= Lambda expressions and function definition =

LFE has lambda, just like Common Lisp. It also, however, has lambda-match to account for Erlang's pattern-matching abilities in anonymous function calls.

Erlang idioms in LFE

This section does not represent a complete comparison between Erlang and LFE, but should give a taste.

= Pattern matching =

Erlang:

1> {Len,Status,Msg} = {8,ok,"Trillian"}.

{8,ok,"Trillian"}

2> Msg.

"Trillian"

LFE:

lfe> (set (tuple len status msg) #(8 ok "Trillian"))

lfe> ;; or with LFE literal tuple syntax:

lfe> (set `#(,len ,status ,msg) #(8 ok "Trillian"))

#(8 ok "Trillian")

lfe> msg

"Trillian"

= List comprehensions =

Erlang:

1> [trunc(math:pow(3,X)) || X <- [0,1,2,3]].

[1,3,9,27]

LFE:

lfe> (list-comp

((<- x '(0 1 2 3)))

(trunc (math:pow 3 x)))

(1 3 9 27)

Or idiomatic functional style:

lfe> (lists:map

(lambda (x) (trunc (math:pow 3 x)))

'(0 1 2 3))

(1 3 9 27)

= Guards =

Erlang:

right_number(X) when X == 42; X == 276709 ->

true;

right_number(_) ->

false.

LFE:

(defun right-number?

((x) (when (orelse (== x 42) (== x 276709)))

'true)

((_) 'false))

= cons'ing in function heads =

Erlang:

sum(L) -> sum(L,0).

sum([], Total) -> Total;

sum([H|T], Total) -> sum(T, H+Total).

LFE:

(defun sum (l) (sum l 0))

(defun sum

(('() total) total)

(((cons h t) total) (sum t (+ h total))))

or using a ``cons`` literal instead of the constructor form:

(defun sum (l) (sum l 0))

(defun sum

(('() total) total)

((`(,h . ,t) total) (sum t (+ h total))))

= Matching records in function heads =

Erlang:

handle_info(ping, #state {remote_pid = undefined} = State) ->

gen_server:cast(self(), ping),

{noreply, State};

handle_info(ping, State) ->

{noreply, State};

LFE:

(defun handle_info

(('ping (= (match-state remote-pid 'undefined) state))

(gen_server:cast (self) 'ping)

`#(noreply ,state))

(('ping state)

`#(noreply ,state)))

= Receiving messages =

Erlang:

universal_server() ->

receive

{become, Func} ->

Func()

end.

LFE:

(defun universal-server ()

(receive

((tuple 'become func)

(funcall func))))

or:

(defun universal-server ()

(receive

(`#(become ,func)

(funcall func))))

Examples

= Erlang interoperability =

Calls to Erlang functions take the form (: ... ):

(io:format "Hello, World!")

= Functional paradigm =

Using recursion to define the Ackermann function:

(defun ackermann

((0 n) (+ n 1))

((m 0) (ackermann (- m 1) 1))

((m n) (ackermann (- m 1) (ackermann m (- n 1)))))

Composing functions:

(defun compose (f g)

(lambda (x)

(funcall f

(funcall g x))))

(defun check ()

(let* ((sin-asin (compose #'sin/1 #'asin/1))

(expected (sin (asin 0.5)))

(compose-result (funcall sin-asin 0.5)))

(io:format "Expected answer: ~p~n" (list expected))

(io:format "Answer with compose: ~p~n" (list compose-result))))

= Concurrency =

Message-passing with Erlang's light-weight "processes":

(defmodule messenger-back

(export (print-result 0) (send-message 2)))

(defun print-result ()

(receive

((tuple pid msg)

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

(io:format "Sending message to process ~p ...~n" (list pid))

(! pid (tuple msg))

(print-result))))

(defun send-message (calling-pid msg)

(let ((spawned-pid (spawn 'messenger-back 'print-result ())))

(! spawned-pid (tuple calling-pid msg))))

Multiple simultaneous HTTP requests:

(defun parse-args (flag)

"Given one or more command-line arguments, extract the passed values.

For example, if the following was passed via the command line:

$ erl -my-flag my-value-1 -my-flag my-value-2

One could then extract it in an LFE program by calling this function:

(let ((args (parse-args 'my-flag)))

...

)

In this example, the value assigned to the arg variable would be a list

containing the values my-value-1 and my-value-2."

(let ((`#(ok ,data) (init:get_argument flag)))

(lists:merge data)))

(defun get-pages ()

"With no argument, assume 'url parameter was passed via command line."

(let ((urls (parse-args 'url)))

(get-pages urls)))

(defun get-pages (urls)

"Start inets and make (potentially many) HTTP requests."

(inets:start)

(plists:map

(lambda (x)

(get-page x)) urls))

(defun get-page (url)

"Make a single HTTP request."

(let* ((method 'get)

(headers '())

(request-data `#(,url ,headers))

(http-options ())

(request-options '(#(sync false))))

(httpc:request method request-data http-options request-options)

(receive

(`#(http #(,request-id #(error ,reason)))

(io:format "Error: ~p~n" `(,reason)))

(`#(http #(,request-id ,result))

(io:format "Result: ~p~n" `(,result))))))

References

{{reflist|1}}