How can I spot subtle Lisp syntax mistakes?
- by Marius Andersen
I'm a newbie playing around with Lisp (actually, Emacs Lisp). It's a lot of fun, except when I seem to run into the same syntax mistakes again and again.
For instance, here's something I've encountered several times. I have some cond form, like
(cond
((foo bar)
(qux quux))
((or corge
(grault warg))
(fred)
(t
xyzzy)))
and the default clause, which returns xyzzy, is never carried out, because it's actually nested inside the previous clause:
(cond
((foo bar)
(qux quux))
((or corge
(grault warg))
(fred))
(t
xyzzy))
It's difficult for me to see such errors when the difference in indentation is only one space. Does this get easier with time?
I also have problems when there's a large distance between the (mal-)indented line and the line it should be indented against. let forms with a lot of complex bindings, for example, or an unless form with a long conditional:
(defun test ()
(unless (foo bar
(qux quux)
(or corge
(grault warg)
(fred))))
xyzzy)
It turns out xyzzy was never inside the unless form at all:
(defun test ()
(unless (foo bar
(qux quux)
(or corge
(grault warg)
(fred)))
xyzzy))
I auto-indent habitually and use parenthesis highlighting to avoid counting parentheses. For the most part it works like a breeze, but occasionally, I discover my syntax mistakes only by debugging. What can I do?