27 Nov 2013

Although it initially takes some getting used to, I think Clojure/Lisp's prefix operators are a great idea. They make multi-argument functions really easy, so you can say things like:

(= a b c)

…instead of the much-clunkier:

((a == b) && (b == c))

After a while you start to wonder why languages bother with operators that are limited to two arguments.

The < and > variants are equally cool, as you can do things like replace the classic range-check:

if ((0 <= x) && (x <= 10)) ...

…with:

(if (<= 0 x 10) ...)

But in the early days, I found it hard to remember whether I wanted (< x 2) or (> x 2). For the simple pair, greater-than or less-than just seemed to naturally belong in the middle, for the visual reminder.

So here's my mind-hack tip: Think of them as 'getting-larger' and 'getting-smaller' operators, and look at the shapes of the characters.

The glyph < is a triangle that starts small on the left and gets larger as it goes right. (< 2 x) is checking if the arguments start small on the left, and get larger as you go right. Two is small, x is larger.

Similarly for >: It starts large on the left of the shape & gets smaller as it goes right.

I found that helped the penny drop for me. Hopefully it'll help a reader or two…

comments powered by Disqus