{"id":2057,"date":"2020-07-19T15:52:45","date_gmt":"2020-07-19T10:22:45","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2057"},"modified":"2020-07-19T15:52:45","modified_gmt":"2020-07-19T10:22:45","slug":"calling-lisp-functions-from-elixir","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/19\/calling-lisp-functions-from-elixir\/","title":{"rendered":"Calling Lisp Functions from Elixir"},"content":{"rendered":"<p>In the <a href=\"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/05\/closure-in-lisp-vs-elixir\/\" target=\"_blank\" rel=\"noopener noreferrer\"><em><strong>last article<\/strong><\/em><\/a>\u00a0I showed how we can simulate the idea of Lisp&#8217;s <em><strong>&#8220;closure&#8221;<\/strong><\/em> in <em><strong>Elixir<\/strong><\/em>. Today, I would like to demonstrate how we can call <em><strong>Lisp<\/strong><\/em> functions from <em><strong>Elixir<\/strong><\/em> using the <em><strong>NIF<\/strong><\/em> interface.<\/p>\n<p>What is the need to integrate <em><strong>Elixir<\/strong><\/em> with another language? I can think of two reasons:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">1) We have a library written in another language and we want to reuse that library in <em><strong>Elixir<\/strong><\/em> instead of re-writing the entire functionality in <em><strong>Elixir<\/strong><\/em>.<span class=\"Apple-converted-space\">\u00a0<\/span><\/span><\/p>\n<p><span style=\"color: #0000ff;\">2) The other language has some nice features for solving a specific problem and so we want to solve that problem in that language and use that from <em><strong>Elixir<\/strong><\/em>. For instance, it is easy to implement knowledge-based expert systems using <a href=\"http:\/\/www.lispworks.com\/products\/knowledgeworks.html\" target=\"_blank\" rel=\"noopener noreferrer\"><em><strong>KnowledgeWorks<\/strong><\/em><\/a> module of <em><strong>LispWorks<\/strong><\/em>. So I might implement an expert system in <em><strong>LispWorks<\/strong><\/em> Lisp and drive it from <em><strong>Elixir<\/strong><\/em>. In the same vein, <em><strong>Prolog<\/strong><\/em> might be your choice if you wish to take advantage of the backtracking-based reasoning available in the language. In essence, the strategy is to choose a language that is most suitable for solving (possibly, a subset of) the given problem and use <em><strong>Elixir<\/strong><\/em> for what it is good at, say distributed fault-tolerant computing, etc.<\/span><\/p><\/blockquote>\n<p>Any drawbacks? Yes, of course. The following two points are to be carefully considered before implementing some functionality in <em><strong>NIF<\/strong><\/em>:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">1) Since <em><strong>NIF<\/strong><\/em> operates at the lowest level, it is quite easy to crash the entire <em><strong>VM<\/strong><\/em> if something goes wrong! Rigorous testing is required before the code is made available for use.<\/span><\/p>\n<p><span style=\"color: #0000ff;\">2) There is considerable overhead in calling a function implemented as <em><strong>NIF<\/strong><\/em> (you will see this later when you actually see the implementation). So there must be a justifiable reason to use <em><strong>NIF<\/strong><\/em> instead of a direct <em><strong>Elixir<\/strong><\/em> implementation.<\/span><\/p><\/blockquote>\n<p>Having put forth the pros and cons of the approach, let us continue with our discussion on how to use <em><strong>NIF<\/strong><\/em>.<\/p>\n<p>The integration with the <em><strong>&#8220;foreign&#8221;<\/strong><\/em> language happens through <em><strong>NIF<\/strong><\/em> (<em><strong>Native Implemented Functions<\/strong><\/em>). So, if the <em><strong>&#8220;foreign&#8221;<\/strong><\/em> language supports <em><strong>&#8220;C&#8221;<\/strong><\/em> bindings, then it is possible to integrate it with <em><strong>Elixir<\/strong><\/em>.<\/p>\n<p>The official <a href=\"http:\/\/erlang.org\/doc\/tutorial\/nif.html\" target=\"_blank\" rel=\"noopener noreferrer\"><em><strong>Erlang NIF<\/strong><\/em><\/a> tutorial is here.<span class=\"Apple-converted-space\">\u00a0 <\/span>And <a href=\"https:\/\/andrealeopardi.com\/posts\/using-c-from-elixir-with-nifs\/\" target=\"_blank\" rel=\"noopener noreferrer\"><em><strong>this<\/strong><\/em><\/a>\u00a0is another nice article on <em><strong>NIF<\/strong><\/em>.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Today&#8217;s focus is on learning how to call functions written in <em><strong>Lisp<\/strong><\/em> (specifically, <em><strong>LispWorks<\/strong> <strong>Lisp<\/strong><\/em> on <em><strong>Windows<\/strong><\/em> platform).<\/p>\n<p>The following diagram shows how the integration works.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Elixi-Lisp-Flow.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"2058\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/19\/calling-lisp-functions-from-elixir\/elixi-lisp-flow\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Elixi-Lisp-Flow.jpg\" data-orig-size=\"914,522\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1595142750&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"\" data-image-description=\"&lt;p&gt;Elixi-Lisp Flow&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Elixi-Lisp Flow&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Elixi-Lisp-Flow.jpg\" class=\"aligncenter wp-image-2058\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Elixi-Lisp-Flow.jpg?resize=500%2C286&#038;ssl=1\" alt=\"Elixir-Lisp Flow\" width=\"500\" height=\"286\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Elixi-Lisp-Flow.jpg?w=914&amp;ssl=1 914w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Elixi-Lisp-Flow.jpg?resize=300%2C171&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Elixi-Lisp-Flow.jpg?resize=768%2C439&amp;ssl=1 768w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<h3>Step-1: Build the Lisp DLL<\/h3>\n<p>The functionality that we want to use from <em><strong>Lisp<\/strong><\/em> must be available as <em><strong>&#8220;exported&#8221;<\/strong><\/em> functions in a DLL.<\/p>\n<p>In an <a href=\"https:\/\/www.rangakrish.com\/index.php\/2018\/06\/10\/calling-lisp-functions-from-go-language\/\" target=\"_blank\" rel=\"noopener noreferrer\"><em><strong>earlier article<\/strong><\/em><\/a>, I described the procedure to create a DLL of exported functions from <em><strong>LispWorks<\/strong> <strong>Lisp<\/strong><\/em>. You may find it useful to review that.<\/p>\n<p>Here is the <em><strong>Lisp<\/strong><\/em> code that exports two functions:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">1) Function that adds two integers<\/span><\/p>\n<p><span style=\"color: #0000ff;\">2) Function that takes a string representing Lisp code and evaluates that code. For simplicity, I am assuming that the Lisp code returns an integer and not an arbitrary S-expression.<\/span><\/p><\/blockquote>\n<figure id=\"attachment_2060\" aria-describedby=\"caption-attachment-2060\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Lisp-code-1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2060\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/19\/calling-lisp-functions-from-elixir\/lisp-code-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Lisp-code-1.jpg\" data-orig-size=\"1074,1030\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1595144065&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Lisp Functions\" data-image-description=\"&lt;p&gt;Lisp Functions&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Lisp Functions&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Lisp-code-1-1024x982.jpg\" class=\"wp-image-2060\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Lisp-code-1.jpg?resize=650%2C623&#038;ssl=1\" alt=\"Lisp Functions\" width=\"650\" height=\"623\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Lisp-code-1.jpg?resize=1024%2C982&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Lisp-code-1.jpg?resize=300%2C288&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Lisp-code-1.jpg?resize=768%2C737&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Lisp-code-1.jpg?w=1074&amp;ssl=1 1074w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-2060\" class=\"wp-caption-text\"><strong>Lisp Functions<\/strong><\/figcaption><\/figure>\n<h3>Step-2: Implement the NIF Layer in C Language<\/h3>\n<p>We need an intermediate <em><strong>NIF<\/strong><\/em> layer to marshall the parameters and return values between <em><strong>Elixir<\/strong><\/em> and <em><strong>Lisp<\/strong><\/em> code.<\/p>\n<p>The following is the first part of the <em><strong>NIF<\/strong><\/em> layer.<\/p>\n<figure id=\"attachment_2061\" aria-describedby=\"caption-attachment-2061\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/C-code1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2061\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/19\/calling-lisp-functions-from-elixir\/c-code1\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/C-code1.jpg\" data-orig-size=\"1072,993\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1595145095&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"C Code\" data-image-description=\"&lt;p&gt;C Code&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;C Code&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/C-code1-1024x949.jpg\" class=\"wp-image-2061\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/C-code1.jpg?resize=650%2C602&#038;ssl=1\" alt=\"C Code\" width=\"650\" height=\"602\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/C-code1.jpg?resize=1024%2C949&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/C-code1.jpg?resize=300%2C278&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/C-code1.jpg?resize=768%2C711&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/C-code1.jpg?w=1072&amp;ssl=1 1072w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-2061\" class=\"wp-caption-text\"><strong>Glue Code in C<\/strong><\/figcaption><\/figure>\n<p>The function <em><strong>\u201clisp_init_nif\u201d<\/strong><\/em> initializes the <em><strong>Lisp<\/strong><\/em> environment from the DLL. The second function <em><strong>\u201clisp_quit_nif\u201d<\/strong><\/em> is used to ensure that the <em><strong>Lisp<\/strong><\/em> environment is shutdown properly when not required anymore. Later on, we will see how these are used in <em><strong>Elixir<\/strong><\/em>.<\/p>\n<p>The two main functions are shown below:<\/p>\n<figure id=\"attachment_2062\" aria-describedby=\"caption-attachment-2062\" style=\"width: 649px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/C-code2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2062\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/19\/calling-lisp-functions-from-elixir\/c-code2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/C-code2.jpg\" data-orig-size=\"1087,805\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1595145136&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"C Code (Contd.)\" data-image-description=\"&lt;p&gt;C Code (Contd.)&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;C Code (Contd.)&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/C-code2-1024x758.jpg\" class=\"wp-image-2062\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/C-code2.jpg?resize=649%2C481&#038;ssl=1\" alt=\"C Code (Contd.)\" width=\"649\" height=\"481\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/C-code2.jpg?resize=1024%2C758&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/C-code2.jpg?resize=300%2C222&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/C-code2.jpg?resize=768%2C569&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/C-code2.jpg?w=1087&amp;ssl=1 1087w\" sizes=\"(max-width: 649px) 100vw, 649px\" \/><\/a><figcaption id=\"caption-attachment-2062\" class=\"wp-caption-text\"><strong>C Code (Contd.)<\/strong><\/figcaption><\/figure>\n<p>As you can guess, the two functions <em><strong>&#8220;lisp_add_nif&#8221;<\/strong><\/em> and <em><strong>&#8220;lisp_eval_nif&#8221;<\/strong><\/em> provide the required mapping between <em><strong>Elixir<\/strong><\/em> and the corresponding <em><strong>Lisp<\/strong><\/em> function parameters as well as the return values.<\/p>\n<p>The array <em><strong>\u201cnif_funcs\u201d<\/strong><\/em> defined at the end sets up the mapping between all the <em><strong>Elixir<\/strong><\/em> and <em><strong>NIF<\/strong><\/em> functions.<\/p>\n<p>Now that the functions are defined, we have to create a DLL. Here is how we use the <em><strong>Visual Studio<\/strong><\/em> command line tool to generate the DLL:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">cl<span class=\"Apple-converted-space\">\u00a0 <\/span>-I<span class=\"Apple-converted-space\">\u00a0 <\/span>&#8220;G:\\Program Files\\erl10.3\\usr\\include&#8221;<span class=\"Apple-converted-space\">\u00a0 <\/span>-LD -MD -Fe: lisp_nif.dll lisp_nif.c<\/span><span class=\"Apple-converted-space\">\u00a0<\/span><\/p><\/blockquote>\n<p>Remember that this must be run from the <em><strong>Developer command prompt<\/strong><\/em>.<\/p>\n<h3>Step-3: Load the Native Functions in Elixir<\/h3>\n<p>The final step is to define the appropriate <em><strong>Module<\/strong><\/em> in <em><strong>Elixir<\/strong><\/em> and load the <em><strong>NIF<\/strong><\/em>. The hook <em><strong>\u201c@load\u201d<\/strong><\/em> helps here. Here is the code for our example:<\/p>\n<figure id=\"attachment_2063\" aria-describedby=\"caption-attachment-2063\" style=\"width: 508px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Elixir-code.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2063\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/19\/calling-lisp-functions-from-elixir\/elixir-code\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Elixir-code.jpg\" data-orig-size=\"616,1242\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1595155612&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Elixir Loader\" data-image-description=\"&lt;p&gt;Elixir Loader&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Elixir Loader&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Elixir-code-508x1024.jpg\" class=\"size-large wp-image-2063\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Elixir-code.jpg?resize=508%2C1024&#038;ssl=1\" alt=\"Elixir Loader\" width=\"508\" height=\"1024\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Elixir-code.jpg?resize=508%2C1024&amp;ssl=1 508w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Elixir-code.jpg?resize=149%2C300&amp;ssl=1 149w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Elixir-code.jpg?w=616&amp;ssl=1 616w\" sizes=\"(max-width: 508px) 100vw, 508px\" \/><\/a><figcaption id=\"caption-attachment-2063\" class=\"wp-caption-text\"><strong>Elixir Loader<\/strong><\/figcaption><\/figure>\n<p>A brief explanation may be useful here. Our <em><strong>Lisp<\/strong><\/em> function <em><strong>\u201ceval\u201d<\/strong><\/em> takes a string of <em><strong>S-expressions<\/strong><\/em> and returns an <em><strong>integer<\/strong><\/em>. The correct way to call this from <em><strong>Elixir<\/strong><\/em> is to pass a character list, for example, <em><strong>\u2018(+ 10 20)\u2019<\/strong><\/em>. But someone might pass a double-quoted string literal instead. To handle this correctly, I have defined the function <em><strong>\u201cexec\u201d<\/strong><\/em> with two argument types. One takes a double-quoted string and converts it into a character list before passing it to <em><strong>\u201ceval\u201d<\/strong><\/em>. The other passes the argument directly to <em><strong>\u201ceval\u201d<\/strong><\/em>.<span class=\"Apple-converted-space\">\u00a0This is just a convenience.<\/span><\/p>\n<p>Here is a session in <em><strong>IEX<\/strong><\/em>:<\/p>\n<figure id=\"attachment_2064\" aria-describedby=\"caption-attachment-2064\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2064\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/19\/calling-lisp-functions-from-elixir\/session-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg\" data-orig-size=\"968,532\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1595167343&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Using the Lisp Functions\" data-image-description=\"&lt;p&gt;Using the Lisp Functions&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Using the Lisp Functions&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg\" class=\"wp-image-2064\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg?resize=650%2C357&#038;ssl=1\" alt=\"Using the Lisp Functions\" width=\"650\" height=\"357\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg?w=968&amp;ssl=1 968w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg?resize=300%2C165&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg?resize=768%2C422&amp;ssl=1 768w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-2064\" class=\"wp-caption-text\"><strong>Using the Lisp Functions<\/strong><\/figcaption><\/figure>\n<p>First, we compile and load the <em><strong>NIF<\/strong><\/em>. The call <em><strong>\u201cLisp.init\u201d<\/strong><\/em> initializes the <em><strong>Lisp<\/strong><\/em> environment by properly loading the function entry points. <em><strong>\u201cLisp.exec\u201d<\/strong><\/em> and <em><strong>\u201cLisp.eval\u201d<\/strong><\/em> are then used to evaluate various <em><strong>Lisp<\/strong><\/em> expressions. Take note of the complex multi-line <em><strong>\u201cflet\u201d<\/strong><\/em> expression and how it is evaluated correctly.<\/p>\n<p>When we do not require the <em><strong>Lisp<\/strong><\/em> environment anymore, it is a good idea to call <em><strong>\u201cLisp.quit\u201d<\/strong><\/em>.<\/p>\n<p>The brief interactive session in <em><strong>IEX<\/strong><\/em> demonstrates that the integration between <em><strong>Elixir<\/strong><\/em> and <em><strong>Lisp<\/strong><\/em> works as expected. Hopefully, you understood the steps involved in the process.<\/p>\n<p>For the record, I used <em><strong>LispWorks 7.1.1<\/strong> <strong>64-bit Enterprise Edition for Windows<\/strong><\/em> and <em><strong>Visual Studio 2019 Professional<\/strong> <\/em>(version 16.6.4) for today\u2019s experiment. <em><strong>Elixir<\/strong><\/em> version is <em><strong>1.10.3 for Windows<\/strong><\/em>.<\/p>\n<p><a href=\"http:\/\/www.rangakrish.com\/downloads\/Elixir-Lisp.zip\" target=\"_blank\" rel=\"noopener noreferrer\"><em><strong>Here<\/strong><\/em><\/a>\u00a0is the source code used in the example.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Have a great weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last article\u00a0I showed how we can simulate the idea of Lisp&#8217;s &#8220;closure&#8221; in Elixir. Today, I would like to demonstrate how we can call Lisp functions from Elixir using the NIF interface. What is the need to integrate Elixir with another language? I can think of two reasons: 1) We have a library [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[242,18,17],"tags":[243,19,245],"class_list":["post-2057","post","type-post","status-publish","format-standard","hentry","category-elixir","category-lisp","category-programming","tag-elixir","tag-lisp","tag-nif"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-xb","jetpack-related-posts":[{"id":2071,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/08\/01\/elixir-wrapping-lisp-calls-in-a-macro\/","url_meta":{"origin":2057,"position":0},"title":"Elixir: Wrapping Lisp Calls in a Macro","author":"admin","date":"August 1, 2020","format":false,"excerpt":"In the last article, I showed how to make calls to Lisp functions from Elixir. We followed this pattern: 1) Initialize the Lisp environment by calling Lisp.init 2) Call Lisp functions as needed 3) Free the Lisp environment by calling Lisp.quit Using the powerful macro programming capabilities of Elixir, we\u2026","rel":"","context":"In &quot;Elixir&quot;","block_context":{"text":"Elixir","link":"https:\/\/www.rangakrish.com\/index.php\/category\/elixir\/"},"img":{"alt_text":"Sample Session","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/08\/Session.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/08\/Session.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/08\/Session.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/08\/Session.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2042,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/05\/closure-in-lisp-vs-elixir\/","url_meta":{"origin":2057,"position":1},"title":"Closure in Lisp vs Elixir","author":"admin","date":"July 5, 2020","format":false,"excerpt":"I started learning Elixir\u00a0a week ago. Although this has been at the top of my To-do list for quite a while, I couldn't take it up due to other commitments.\u00a0 I love Elixir. It is a great functional programming language. Having programmed in Lisp for a long time, I immediately\u2026","rel":"","context":"In &quot;Elixir&quot;","block_context":{"text":"Elixir","link":"https:\/\/www.rangakrish.com\/index.php\/category\/elixir\/"},"img":{"alt_text":"Program Output","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output2.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output2.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output2.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output2.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2084,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/08\/16\/pattern-matching-comparing-elixir-and-mathematica\/","url_meta":{"origin":2057,"position":2},"title":"Pattern Matching: Comparing Elixir and Mathematica","author":"admin","date":"August 16, 2020","format":false,"excerpt":"One of the things I like about Elixir\u00a0is its support for patterns at the core language level, not through library functions as in most other languages. This contributes to writing cleaner code, in my opinion. \u00a0 Another environment that I am familiar with, namely Mathematica, boasts of (arguably) the most\u2026","rel":"","context":"In &quot;Elixir&quot;","block_context":{"text":"Elixir","link":"https:\/\/www.rangakrish.com\/index.php\/category\/elixir\/"},"img":{"alt_text":"Symbolic Expressions","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/08\/pattern-mm.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/08\/pattern-mm.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/08\/pattern-mm.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2645,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/12\/25\/calling-lisp-functions-from-d-language\/","url_meta":{"origin":2057,"position":3},"title":"Calling Lisp Functions from D Language","author":"admin","date":"December 25, 2021","format":false,"excerpt":"After exploring \"newLisp\" in the past few posts, I would like to start looking at the \"D Programming Language\"\u00a0(DLang). DLang\u00a0has been around since 2001. It was originally created by Walter Bright and later Andrei Alexandrescu joined the team in 2007. The main inspiration for DLang was C++, although it uses\u2026","rel":"","context":"In &quot;D Language&quot;","block_context":{"text":"D Language","link":"https:\/\/www.rangakrish.com\/index.php\/category\/d-language\/"},"img":{"alt_text":"Lisp Function Called from D","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/12\/Lisp-300x107.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/12\/Lisp-300x107.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/12\/Lisp-300x107.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3035,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/03\/05\/julia-defining-functions-dynamically\/","url_meta":{"origin":2057,"position":4},"title":"Julia: Defining Functions Dynamically","author":"admin","date":"March 5, 2023","format":false,"excerpt":"One of the widely discussed features of Julia is its support for metaprogramming. This feature makes it possible to generate and inspect the code at runtime. In essence, metaprogramming blurs the distinction between code and data. When used carefully, it can contribute to good code. Of course, Julia is not\u2026","rel":"","context":"In &quot;Julia&quot;","block_context":{"text":"Julia","link":"https:\/\/www.rangakrish.com\/index.php\/category\/julia\/"},"img":{"alt_text":"Defining A Function Dynamically","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example1-300x195.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example1-300x195.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example1-300x195.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":963,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/06\/24\/calling-go-functions-from-lisp-part-2\/","url_meta":{"origin":2057,"position":5},"title":"Calling Go Functions from Lisp &#8211; Part 2","author":"admin","date":"June 24, 2018","format":false,"excerpt":"In my earlier article Calling Go Functions from Lisp, I explained the steps for making calls to Go functions from another language, specifically LispWorks Lisp. Today, I want to give a slightly more\u00a0 interesting example showing the use of Go channels\u00a0through exported functions. Go is widely admired for its native\u2026","rel":"","context":"In &quot;Golang&quot;","block_context":{"text":"Golang","link":"https:\/\/www.rangakrish.com\/index.php\/category\/golang\/"},"img":{"alt_text":"Go Functions","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/06\/Go-Functions.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=2057"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2057\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}