Quantcast
Channel: On Code
Viewing all articles
Browse latest Browse all 21

Case Conversion Considered Useful

$
0
0
One of the stranger[1] features of Common Lisp is that the CL reader, by default, will convert symbols to uppercase[2] which can be a little surprising to newcomers, and inevitably leads misconceptions regarding case (in)sensitivity.

One of the advantages of this automatic case conversion is that it allows us to use case as syntactic markers.

Here's a simple example (ignore the non idiomatic use of CL).

(defun filter (test list)
(let ((result ()))
(dolist (elt list result)
(when (funcall test elt)
(push elt result)))))

Did you notice the return form?
This happens to be one of those constructs that is quite easy to overlook when reading through code, it doesn't happen often but it does happen[3].

However, if we change it to this.

(defun filter (test list)
(let ((result ()))
(dolist (elt list RESULT)
(when (funcall test elt)
(push elt result)))))
the result form 'leaps' out of the page which makes it very difficult to miss.

It's the code equivalent of wearing a silly hat.

---

1: Stranger, as in, 'This isn't like C/Java/Python/Ruby'.
2: This is only the default and can be changed using readtable-case
3: Well it happens to me, ok.


Viewing all articles
Browse latest Browse all 21

Trending Articles