Using Pcase and Name-let for Pattern Matching and Tail Recursion

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. Scheme famously has it in its specification and many Lisps have it de facto if not de jure. If you’re not used to using recursion in your programming, that may seem like hyperbole but once you’ve internalized the method, other paradigms seem clumsy. Tail recursion is not really built into Elisp but it’s still possible using the named-let construct. It’s basically the same as Scheme’s named-let so if you’re a Schemer you’ll feel right at home.

Sadly, I haven’t internalized the pattern matching paradigm although I keep promising myself to do so. It’s a powerful idea that extends things like C’s switch statement or Lisp’s cond and case constructs. In Elisp, you use the pcase construct for this. Once you get used to the pattern syntax, it’s a powerful generalization of the common “if-else” constructs most programming languages have.

Arneson’s examples don’t begin to capture the power of these methods. They’re powerful tools that are well worth learning and internalizing. If you’re an Emacser, Elisp provides you an easy way of experimenting with these techniques. Believe me, mastering them will make you a better programmer.

This entry was posted in General and tagged , . Bookmark the permalink.