LFE | |
Paradigm: | Multi-paradigm |
Family: | Erlang, Lisp |
Designer: | Robert Virding |
Developer: | Robert Virding |
Latest Release Version: | 2.1.1 |
Typing: | dynamic, strong |
Programming Language: | Erlang |
Operating System: | Cross-platform |
License: | Apache 2.0 |
File Ext: | .lfe .hrl |
File Formats: | --> |
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).[1] LFE is actively supported on all recent releases of Erlang; the oldest version of Erlang supported is R14.
Initial work on LFE began in 2007, when Robert Virding started creating a prototype of Lisp running on Erlang.[2] 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.[2]
Virding announced the first release of LFE on the Erlang Questions mail list in March 2008.[3] This release of LFE was very limited: it did not handle recursive letrec
s, binary
s, receive
, or try
; it also did not support a Lisp shell.
Initial development of LFE was done with version R12B-0 of Erlang[4] on a Dell XPS laptop.[5]
Robert Virding has stated that there were several reasons why he started the LFE programming language:[2]
lfe
and lfescript
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.
In LFE, the list data type is written with its elements separated by whitespace, and surrounded by parentheses. For example, is a list whose elements are the integers and, and the atom . 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.
The LFE-Erlang operators are used in the same way. The expression
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.
This section does not represent a complete comparison between Erlang and LFE, but should give a taste.
Erlang:
Erlang:
Erlang:
276709 -> true; right_number(_) -> false.LFE:
x 276709))) 'true) ((_) 'false))
Erlang:
Erlang:
Erlang:
Calls to Erlang functions take the form (
Using recursion to define the Ackermann function:
Composing functions:
(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))))
Message-passing with Erlang's light-weight "processes":
(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:
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))))))