{"id":10978,"date":"2022-11-27T13:54:52","date_gmt":"2022-11-27T18:54:52","guid":{"rendered":"https:\/\/irreal.org\/blog\/?p=10978"},"modified":"2022-11-27T13:54:52","modified_gmt":"2022-11-27T18:54:52","slug":"using-pcase-and-name-let-for-pattern-matching-and-tail-recursion","status":"publish","type":"post","link":"https:\/\/irreal.org\/blog\/?p=10978","title":{"rendered":"Using Pcase and Name-let for Pattern Matching and Tail Recursion"},"content":{"rendered":"<p>Erik L. Arneson has an interesting post about using <a href=\"https:\/\/arnesonium.com\/2022\/11\/pattern-matching-tail-recursion-emacs-lisp\">functional programming constructs with Elisp<\/a>. In particular, he considers pattern matching and tail recursion.<\/p>\n<p>Tail recursion is, in my estimation, one of the most useful properties a programming language can have. Scheme famously has it in its specification and many Lisps have it <i>de facto<\/i> if not <i>de jure<\/i>. If you\u2019re not used to using recursion in your programming, that may seem like hyperbole but once you\u2019ve internalized the method, other paradigms seem clumsy. Tail recursion is not really built into Elisp but it\u2019s still possible using the <code>named-let<\/code> construct. It\u2019s basically the same as Scheme\u2019s <code>named-let<\/code> so if you\u2019re a Schemer you\u2019ll feel right at home.<\/p>\n<p>Sadly, I haven\u2019t internalized the pattern matching paradigm although I keep promising myself to do so. It\u2019s a powerful idea that extends things like C\u2019s <code>switch<\/code> statement or Lisp\u2019s <code>cond<\/code> and <code>case<\/code> constructs. In Elisp, you use the <code>pcase<\/code> construct for this. Once you get used to the pattern syntax, it\u2019s a powerful generalization of the common \u201cif-else\u201d constructs most programming languages have.<\/p>\n<p>Arneson\u2019s examples don\u2019t begin to capture the power of these methods. They\u2019re powerful tools that are well worth learning and internalizing. If you\u2019re an Emacser, Elisp provides you an easy way of experimenting with these techniques. Believe me, mastering them will make you a better programmer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Erik L. Arneson has an interesting post about using functional programming constructs with Elisp. In particular, he considers pattern matching and tail recursion. Tail recursion is, in my estimation, one of the most useful properties a programming language can have. &hellip; <a href=\"https:\/\/irreal.org\/blog\/?p=10978\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[26,18],"class_list":["post-10978","post","type-post","status-publish","format-standard","hentry","category-general","tag-elisp","tag-emacs"],"_links":{"self":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/10978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10978"}],"version-history":[{"count":1,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/10978\/revisions"}],"predecessor-version":[{"id":10979,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/10978\/revisions\/10979"}],"wp:attachment":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}