{"id":2042,"date":"2020-07-05T10:51:32","date_gmt":"2020-07-05T05:21:32","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2042"},"modified":"2020-07-19T15:53:17","modified_gmt":"2020-07-19T10:23:17","slug":"closure-in-lisp-vs-elixir","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/05\/closure-in-lisp-vs-elixir\/","title":{"rendered":"Closure in Lisp vs Elixir"},"content":{"rendered":"<p>I started learning <a href=\"https:\/\/elixir-lang.org\" target=\"_blank\" rel=\"noopener noreferrer\"><em><strong>Elixir<\/strong><\/em><\/a>\u00a0a week ago. Although this has been at the top of my <em><strong>To-do<\/strong><\/em> list for quite a while, I couldn&#8217;t take it up due to other commitments.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>I love <em><strong>Elixir<\/strong><\/em>. It is a great functional programming language. Having programmed in <em><strong>Lisp<\/strong><\/em> for a long time, I immediately noticed the similarity between <em><strong>Lisp<\/strong><\/em> and <em><strong>Elxir<\/strong><\/em>, especially in the area of <em><strong>Macros<\/strong><\/em>. More on this in a future article&#8230;<\/p>\n<p>The first thing I decided to check was whether <em><strong>Elixir<\/strong><\/em> supports function <em><strong>&#8220;closure&#8221;<\/strong><\/em>, where a function carries state with it. Today&#8217;s article is an attempt to explore this idea.<\/p>\n<p>Take a look at the following <em><strong>Lisp<\/strong><\/em> function:<\/p>\n<figure id=\"attachment_2043\" aria-describedby=\"caption-attachment-2043\" style=\"width: 651px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/lisp-code.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"2043\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/05\/closure-in-lisp-vs-elixir\/lisp-code\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/lisp-code.jpg\" data-orig-size=\"898,465\" 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;1593939258&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=\"Counter in Lisp\" data-image-description=\"&lt;p&gt;Counter in Lisp&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Counter in Lisp&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/lisp-code.jpg\" class=\"wp-image-2043\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/lisp-code.jpg?resize=651%2C337&#038;ssl=1\" alt=\"Counter in Lisp\" width=\"651\" height=\"337\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/lisp-code.jpg?w=898&amp;ssl=1 898w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/lisp-code.jpg?resize=300%2C155&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/lisp-code.jpg?resize=768%2C398&amp;ssl=1 768w\" sizes=\"(max-width: 651px) 100vw, 651px\" \/><\/a><figcaption id=\"caption-attachment-2043\" class=\"wp-caption-text\"><strong>Counter in Lisp<\/strong><\/figcaption><\/figure>\n<p>The function <em><strong>&#8220;counter&#8221;<\/strong><\/em> takes two arguments, an initial value and a <em><strong>delta<\/strong><\/em> to be applied every time. It sets up a local environment, where the variable <em><strong>&#8220;cur&#8221;<\/strong><\/em> is initialized with the passed initial value. It then defines and returns an <em><strong>anonymous<\/strong><\/em> function that increments the local state with <em><strong>&#8220;delta&#8221;<\/strong><\/em>, each time it is called.<\/p>\n<p>The following shows how it is used:<\/p>\n<figure id=\"attachment_2044\" aria-describedby=\"caption-attachment-2044\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/lisp-output.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2044\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/05\/closure-in-lisp-vs-elixir\/lisp-output\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/lisp-output.jpg\" data-orig-size=\"619,595\" 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;1593939122&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 Output\" data-image-description=\"&lt;p&gt;Lisp Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Lisp Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/lisp-output.jpg\" class=\"wp-image-2044\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/lisp-output.jpg?resize=500%2C481&#038;ssl=1\" alt=\"Lisp Output\" width=\"500\" height=\"481\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/lisp-output.jpg?w=619&amp;ssl=1 619w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/lisp-output.jpg?resize=300%2C288&amp;ssl=1 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2044\" class=\"wp-caption-text\"><strong>Lisp Output<\/strong><\/figcaption><\/figure>\n<p>In the first step, we call <em><strong>&#8220;counter&#8221;<\/strong><\/em> passing 10 as the initial value and 3 as the delta. This returns an anonymous function, which we store in <em><strong>&#8220;incr&#8221;<\/strong><\/em>. Subsequently we call the anonymous function and it correctly returns the current value based on the previous state and <em><strong>&#8220;delta&#8221;<\/strong><\/em>. We then obtain another anonymous function, this time based on initial value of 100 and delta of -2. As you can see, the next few steps confirm that this too works as expected.<\/p>\n<p>What this shows is that the anonymous function carries with it the environment in which it was created.<\/p>\n<p>Let us mimic this approach in <em><strong>Elixir<\/strong><\/em>. Here is our first attempt:<\/p>\n<figure id=\"attachment_2045\" aria-describedby=\"caption-attachment-2045\" style=\"width: 573px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-code1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2045\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/05\/closure-in-lisp-vs-elixir\/elixir-code1\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-code1.jpg\" data-orig-size=\"573,370\" 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;1593939538&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 &amp;#8211; First Attempt\" data-image-description=\"&lt;p&gt;Elixir &amp;#8211; First Attempt&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Elixir &amp;#8211; First Attempt&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-code1.jpg\" class=\"size-full wp-image-2045\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-code1.jpg?resize=573%2C370&#038;ssl=1\" alt=\"Elixir - First Attempt\" width=\"573\" height=\"370\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-code1.jpg?w=573&amp;ssl=1 573w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-code1.jpg?resize=300%2C194&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-code1.jpg?resize=140%2C90&amp;ssl=1 140w\" sizes=\"(max-width: 573px) 100vw, 573px\" \/><\/a><figcaption id=\"caption-attachment-2045\" class=\"wp-caption-text\"><strong>Elixir &#8211; First Attempt<\/strong><\/figcaption><\/figure>\n<p>We run this code in <em><strong>&#8220;iex&#8221;,<\/strong><\/em> the interactive shell for <em><strong>Elixir<\/strong><\/em>. Take a look at the output:<\/p>\n<figure id=\"attachment_2046\" aria-describedby=\"caption-attachment-2046\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output1.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2046\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/05\/closure-in-lisp-vs-elixir\/elixir-output1\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output1.jpg\" data-orig-size=\"1173,388\" 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;1593939500&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 Output\" data-image-description=\"&lt;p&gt;Elixir Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Elixir Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output1-1024x339.jpg\" class=\"wp-image-2046\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output1.jpg?resize=650%2C215&#038;ssl=1\" alt=\"Elixir Output\" width=\"650\" height=\"215\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output1.jpg?resize=1024%2C339&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output1.jpg?resize=300%2C99&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output1.jpg?resize=768%2C254&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output1.jpg?w=1173&amp;ssl=1 1173w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-2046\" class=\"wp-caption-text\"><strong>Elixir Output<\/strong><\/figcaption><\/figure>\n<p>First, we compile and load the file into the shell. We get an un-used variable warning, and this itself is a clue that something is not as expected!<\/p>\n<p>We create a counter object (an anonymous function),<span class=\"Apple-converted-space\">\u00a0 <\/span>passing 10 as the initial value and 5 as the <em><strong>delta<\/strong><\/em>. When we call this function repeatedly, we see that it returns the same value each time. This is because the local variable <em><strong>&#8220;cur&#8221;<\/strong><\/em> is passed by <em><strong>value<\/strong><\/em> to our anonymous function and hence is not updated when it is modified within the function &#8211; in contrast to <em><strong>Lisp<\/strong><\/em>.<\/p>\n<p>Since\u00a0<em><strong>&#8220;closure&#8221;<\/strong><\/em> is not directly supported in Elixir, is there a way to implement the <em><strong>counter<\/strong><\/em> abstraction such that it works as in the <em><strong>Lisp<\/strong><\/em> example?<\/p>\n<p><a href=\"https:\/\/elixir-lang.org\/getting-started\/mix-otp\/ets.html\" target=\"_blank\" rel=\"noopener noreferrer\"><em><strong>ETS<\/strong><\/em><\/a>\u00a0or <em><strong>Elixir Term Storage<\/strong><\/em> comes to our rescue. This is a costly approach, but we are not concerned about that here. Here is the code that uses this feature:<\/p>\n<figure id=\"attachment_2047\" aria-describedby=\"caption-attachment-2047\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-code2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2047\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/05\/closure-in-lisp-vs-elixir\/elixir-code2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-code2.jpg\" data-orig-size=\"737,620\" 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;1593939585&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=\"Saving State with ETS\" data-image-description=\"&lt;p&gt;Saving State with ETS&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Saving State with ETS&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-code2.jpg\" class=\"wp-image-2047\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-code2.jpg?resize=650%2C547&#038;ssl=1\" alt=\"Saving State with ETS\" width=\"650\" height=\"547\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-code2.jpg?w=737&amp;ssl=1 737w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-code2.jpg?resize=300%2C252&amp;ssl=1 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-2047\" class=\"wp-caption-text\"><strong>Saving State with ETS<\/strong><\/figcaption><\/figure>\n<p>Here we create a <em><strong>&#8220;named&#8221;<\/strong><\/em> ETS table for storing the current value and the delta. The anonymous function uses this table to keep track of the current counter value and update it when it is invoked.<\/p>\n<p>Here is the output with this change:<\/p>\n<figure id=\"attachment_2048\" aria-describedby=\"caption-attachment-2048\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2048\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/05\/closure-in-lisp-vs-elixir\/elixir-output2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output2.jpg\" data-orig-size=\"873,364\" 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;1593939668&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=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output2.jpg\" class=\"wp-image-2048\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output2.jpg?resize=650%2C271&#038;ssl=1\" alt=\"Program Output\" width=\"650\" height=\"271\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output2.jpg?w=873&amp;ssl=1 873w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output2.jpg?resize=300%2C125&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output2.jpg?resize=768%2C320&amp;ssl=1 768w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-2048\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>Good! This works as expected. By the way, the <em><strong>&#8220;reset&#8221;<\/strong><\/em> function is required. It deletes the <em><strong>ETS<\/strong><\/em> table so that it can be garbage collected by the <em><strong>VM<\/strong><\/em>.<\/p>\n<p>Coming to the main point of this article, <em><strong>Lisp<\/strong><\/em> supports closure, whereas <em><strong>Elixir<\/strong><\/em> does not. But there are many ways in <em><strong>Elixir<\/strong><\/em> &#8211; including the one using <em><strong>ETS<\/strong><\/em> &#8211; that enable state-based programming.<\/p>\n<p>I hope to spend more time understanding <em><strong>Elixir<\/strong><\/em> in the weeks to come, so stay tuned for more such articles!<\/p>\n<p>Download the code from <a href=\"http:\/\/www.rangakrish.com\/downloads\/Counter.zip\" target=\"_blank\" rel=\"noopener noreferrer\"><em><strong>here<\/strong><\/em><\/a>.<\/p>\n<p>Have a nice weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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 noticed the similarity between Lisp [&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":[244,243,19],"class_list":["post-2042","post","type-post","status-publish","format-standard","hentry","category-elixir","category-lisp","category-programming","tag-closure","tag-elixir","tag-lisp"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-wW","jetpack-related-posts":[{"id":2057,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/19\/calling-lisp-functions-from-elixir\/","url_meta":{"origin":2042,"position":0},"title":"Calling Lisp Functions from Elixir","author":"admin","date":"July 19, 2020","format":false,"excerpt":"In the last article\u00a0I showed how we can simulate the idea of Lisp's \"closure\" 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:\u2026","rel":"","context":"In &quot;Elixir&quot;","block_context":{"text":"Elixir","link":"https:\/\/www.rangakrish.com\/index.php\/category\/elixir\/"},"img":{"alt_text":"Using the Lisp Functions","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2071,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/08\/01\/elixir-wrapping-lisp-calls-in-a-macro\/","url_meta":{"origin":2042,"position":1},"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":2645,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/12\/25\/calling-lisp-functions-from-d-language\/","url_meta":{"origin":2042,"position":2},"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":3199,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/09\/04\/simulating-python-zip-in-lisp\/","url_meta":{"origin":2042,"position":3},"title":"Simulating Python Zip in Lisp","author":"admin","date":"September 4, 2023","format":false,"excerpt":"The zip() function in Python is a convenient mechanism for iterating over multiple \u201citerables\u201d in parallel. Looping over lists is a common scenario. Here is the output generated by the above code: Common Lisp does not have such a feature built into the language or as part of the standard\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Python Zip() Feature","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/09\/python-code-300x99.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2084,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/08\/16\/pattern-matching-comparing-elixir-and-mathematica\/","url_meta":{"origin":2042,"position":4},"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":476,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/02\/03\/multi-value-return-in-c-7-0\/","url_meta":{"origin":2042,"position":5},"title":"Multi-value Return in C# 7.0","author":"admin","date":"February 3, 2017","format":false,"excerpt":"One of the nice language enhancements to C# in the latest release (7.0) is the ability to return multiple values from a function. Although one could use Tuples for this purpose, it is not an elegant or efficient approach. For more details, see this article. By the way, returning multiple\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"Multi-value Return in C#","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/02\/C-multi-valie.jpg?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/02\/C-multi-valie.jpg?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/02\/C-multi-valie.jpg?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2042","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=2042"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2042\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}