{"path": "/posts/lfe-lisp-for-the-beam-erlangelixir-c87/", "title": "LFE - Lisp for the BEAM (erlang/Elixir) 2/N · Bruno Dias", "html": "<!doctype html>\n<html lang=\"en\">\n<head>\n  <meta charset=\"utf-8\">\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n  <meta name=\"description\" content=\"Processes, Erlang Behaviors, and OTP.\">\n  <title>LFE - Lisp for the BEAM (erlang/Elixir) 2/N · Bruno Dias</title>\n  <link rel=\"canonical\" href=\"https://diasbruno.github.io/posts/lfe-lisp-for-the-beam-erlangelixir-c87/\" data-post-metadata>\n  <meta name=\"author\" content=\"Bruno Dias\" data-post-metadata>\n  <meta property=\"og:type\" content=\"article\" data-post-metadata>\n  <meta property=\"og:title\" content=\"LFE - Lisp for the BEAM (erlang/Elixir) 2/N\" data-post-metadata>\n  <meta property=\"og:url\" content=\"https://diasbruno.github.io/posts/lfe-lisp-for-the-beam-erlangelixir-c87/\" data-post-metadata>\n  <meta property=\"article:published_time\" content=\"2025-11-11T03:28:37+00:00\" data-post-metadata>\n  <meta property=\"og:image\" content=\"https://diasbruno.github.io/assets/site/substack/lfe-lisp-for-the-beam-erlangelixir-c87/figure-1.jpg\" data-post-metadata>\n  <link rel=\"icon\" href=\"/assets/site/signature.svg\" type=\"image/svg+xml\">\n  <link rel=\"alternate\" type=\"application/atom+xml\" title=\"diasbruno\" href=\"/atom.xml\">\n  <link rel=\"alternate\" type=\"application/json\" href=\"/posts/lfe-lisp-for-the-beam-erlangelixir-c87/content.json\">\n  <link rel=\"stylesheet\" href=\"/assets/site/tokens.css\">\n  <link rel=\"stylesheet\" href=\"/assets/site/site.css\">\n  <link rel=\"stylesheet\" href=\"/assets/site/footer.css\">\n\n</head>\n<body class=\"light\">\n<div id=\"site-page\">\n<a class=\"skip-link\" href=\"#content\">Skip to content</a>\n<header class=\"site-header\">\n  <a class=\"brand\" href=\"/\" data-site-navigation aria-label=\"Bruno Dias, home\">\n    <img src=\"/assets/site/signature.svg\" width=\"24\" height=\"34\" alt=\"\">\n  </a>\n  <nav aria-label=\"Main navigation\">\n    <ul>\n<li><a class=\"nav-vrn\" href=\"/vrn/\" data-site-navigation><span class=\"nav-label\">vrn</span> <span class=\"nav-badge\">research</span></a></li>\n<li><a class=\"nav-posts\" href=\"/posts/\" data-site-navigation aria-current=\"location\">Posts</a></li>\n<li><a class=\"nav-projects\" href=\"/projects/\" data-site-navigation>Projects</a></li>\n<li><a class=\"nav-about\" href=\"/about/\" data-site-navigation>About</a></li>\n</ul>\n  </nav>\n</header>\n\n<main class=\"section\" id=\"content\" tabindex=\"-1\">\n  <article class=\"post-detail\">\n    <header class=\"section-header\">\n      <p class=\"post-category\">Post</p>\n      <h1>LFE - Lisp for the BEAM (erlang/Elixir) 2/N</h1>\n<p class=\"post-subtitle\">Processes, Erlang Behaviors, and OTP.</p>\n      <p><time datetime=\"2025-11-11T03:28:37+00:00\">2025-11-11</time></p>\n    </header>\n    <div class=\"post-columns\">\n      <section class=\"post-body\" aria-label=\"Article\">\n\n<figure><img src=\"/assets/site/substack/lfe-lisp-for-the-beam-erlangelixir-c87/figure-1.jpg\" alt=\"\" loading=\"lazy\" width=\"1456\" height=\"840\">\n</figure>\n<p>Everything from the Erlang and OTP is available on LFE. Although, you can pretty much write code in a more loose form, you can still implements behaviors that are already present on the OTP and others.</p>\n<h3>Processes</h3>\n<p>Receive and send are still the same. Each cause on the <code>receive</code> are still made of a list of pairs in the form (message, handler). The two wildcard option to end a receive, or many forms, is using <code>else</code> or <code>_</code>.</p>\n<pre><code>(receive\n  (’a (io:format “The atom \\”a\\””))\n  ((cons 'a num) (io:format “A list with atom \\”a\\” and a number ~p” (list num)))\n  ((tuple 'a num) (io:format “A tuple with atom \\”a\\” and a number ~p ” (list num)))\n  (else (io:format \"Wildcard pattern. Same as _.\")))</code></pre>\n<p>Sending a message is still the same <code>pid ! message</code>, but with parenthesis <code>(! pid message)</code>. </p>\n<pre><code>(let ((p (spawn (lambda () \n                  (receive \n                    (else (io:format “wildcard case~n”))))))) \n  (! p 1)))</code></pre>\n<h3>Creating a gen_server</h3>\n<p><code>defmodule</code> is a macro that generates 2 forms.</p>\n<pre><code>lfe&gt; (macroexpand ‘(defmodule k))\n(progn (define-module k () ()) (defmacro MODULE () `’k))</code></pre>\n<p>To be more generic just like the ?<code>MODULE</code> in Erlang, we use <code>(MODULE)</code> that is a macro that gives the correct module atom name.</p>\n<pre><code>(defmodule my-app-server\n  (behavior gen_server)\n  (export\n   (init 1)\n   (terminate 2)\n   (start 0)\n   (handle_call 3)\n   (handle_cast 2)))\n\n(defun init (_args)\n  (logger:info “Starting simple gen_server...”)\n  (tuple 'ok '()))\n\n(defun terminate (_reason _state)\n  ‘normal)\n\n(defun start ()\n  (gen_server:start_link (MODULE) (list) (list)))\n\n(defun handle_call (request from state)\n  (tuple 'noreply state))\n\n(defun handle_cast (request state)\n  (tuple 'noreply state))</code></pre>\n<p>It still the same declaration and usage of any behavior like application, supervisor, gen_server and others.</p>\n\n      </section>\n      <aside class=\"post-links\" aria-labelledby=\"post-links-heading\">\n        <h2 id=\"post-links-heading\">Links</h2>\n        <ul>\n<li><a href=\"/posts/\" data-site-navigation>All posts</a></li>\n<li><a href=\"/posts/lfe-lisp-for-the-beam-erlangelixir/\" data-site-navigation>LFE - Lisp for the BEAM (erlang/Elixir) 1/N</a></li>\n<li><a href=\"https://diasbruno.substack.com/p/lfe-lisp-for-the-beam-erlangelixir-c87\">Original on Substack</a></li>\n        </ul>\n      </aside>\n    </div>\n  </article>\n</main>\n\n<footer class=\"site-footer\">\n  <div class=\"site-footer-inner\">\n    <a class=\"site-footer-home\" href=\"/\" data-site-navigation>Bruno Dias</a>\n    <ul aria-label=\"Footer links\">\n      <li><a href=\"https://github.com/diasbruno\">GitHub</a></li>\n      <li><a href=\"/atom.xml\">Atom feed</a></li>\n    </ul>\n  </div>\n</footer>\n\n</div>\n<script type=\"module\" src=\"/assets/site/navigation.js\"></script>\n</body>\n</html>\n"}
