The Loop Macro Explained

If you’re a Lisper, you’re probably aware of the loop macro controversy. Few people are neutral: they either love it or hate it. I’m in the latter group. Although I’ve gotten to where I can mostly read a loop macro and figure out what it’s doing, I would never use it to write code. My objections are two-fold:

  1. Its syntax is not at all Lisp-like and requires a paradigm shift to use.
  2. It’s not really documented. Paul Graham remarks that it’s code is the documentation.

Gavin Freeborn is in the other camp. He loves the loop macro and considers it one of the best parts of Common Lisp. Unlike me, he considers its declarative syntax a feature. He thinks the loop macro is “the best form of procedural iteration that I’ve ever seen.” He has a video on the loop macro that regardless of which camp you fall in you’ll probably find useful.

One thing I really liked about the video is that he precedes each keyword with a colon—something not strictly required—making it much easier to mentally parse the loop invocation. If you’re like me and are not a loop aficionado, you’ll find the video useful because it will help you understand what’s going on when you run across the macro in other people’s code. If you don’t mind the idea of loop but are not an expert, the video serves as a good introduction.

The video is just short of 21 minutes long so you’ll need to schedule some time. If you’re not a Common Lisper but want to experiment with the macro, Elisp also has a version (officially called cl-loop but loop is still accepted) that you can play with from the comfort of Emacs.

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