Iteration with Elisp

John Kitchen has important things to do so naturally he’s procrastinating. That’s probably bad for him but it’s good for us because he has another Elisp video up. This one considers various ways of iterating in Elisp. Earlier, he had posted a video on iteration in Python and this video considers the corresponding idioms in Elisp.

To me, the most general (non-recursive) iteration technique in Lisp is the do statement. It’s so powerful that you can do significant work even with an empty body. It’s powerful but not exactly transparent. Kitchin mostly focuses on the cl-loop macro. It’s natural and seems to translate amazingly well into Python even though Elisp predates Python by many years.

I’ve never been able to warm up to the loop macro but Kitchen’s demonstration of its use is more persuasive than any polemic could be. Take a look at the video to see it in action. It can do all sorts of things in a natural way much as Python does with list comprehensions.

Kitchin also considers using while. You can specify an ending condition or you can exit from the while by throwing an exception. That’s Lisp’s version of the break verb in other languages’ version of while.

The other iteration method that I use a lot that Kitchin doesn’t mention is the dolist macro. It’s sort of like a list comprehension but easier to use. In those situations where Kitchin uses cl-loop, I’d be inclined to use dolist. Still, Kitchin makes a good case for cl-loop and has encouraged me to spend a bit of time getting more comfortable with it.

The video is just short of 24 minutes so you’ll have to put some time aside for it but as usual with Kitchin’s videos, it’s worth it.

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