Xah Lee saw my post about Tim Visher's video on building a multiplication table in Emacs and decided to experiment. Visher's video was in the context of VimGolf where the goal was to build the table in the minimum number of keystrokes. Lee wasn't interested in building the table in the minimum number of keystrokes; he wanted to see how you could do it in various programming languages. He gives solutions in Emacs Lisp, Python, and Perl. That was interesting but then he says
Lisp is a bit verbose for this kinda thing due to the nested syntax.
But you can write it in perl, python, ruby, bash, or any dynamic lang
quickly, and press a button to have emacs call it and insert the
To a Lisper like me this is a challenge that can't go unanswered. Therefore I started to think about how I could code this concisely with Emacs Lisp. My first thought was to use the
dotimes macro but that starts with 0, which complicates things. Then I realized that since we only needed to deal with the numbers 1–10, I could just use
dolist like this
(dolist (r '(1 2 3 4 5 6 7 8 9 10)) (dolist (c '(1 2 3 4 5 6 7 8 9 10)) (insert (format "%2d " (* r c)))) (insert "\n"))
Now put the point at the beginning of the next line, type 【Ctrl+x Ctrl+e】 and the table is inserted into the buffer.
None of this is of any importance, of course, but it is fun and instructive. My solution can be criticized on the grounds that it's really a Common Lisp solution but it does work in Emacs due to the
cl.el package. It's also slightly objectionable to have to list the numbers explicitly but I couldn't think of a better way. Leave a comment if you have a nicer solution.
Update: Added link to Xah Lee's post.