Comment by kazinator

3 years ago

Javascript programmers have to learn frameworks. You cannot escape from the fact that programs extend the language.

When you write a function, or define a new type, you're extending a language.

In human languages, nobody would dispute the idea that the formation of new nouns and verbs extends the language.

Lisp is kind of like a language where you can add a new conjunction and preposition, not just a new noun or verb. To the uninitiated, that somehow seems like it must be harder.

The fundamental problem is that you have some piece of syntax, and it denotes some process (or whatever) which is entirely implicit. Whether that syntax is a new kind of macro-statement or function call is immaterial. If you have no idea what it is, you have to go to the definition.

The behavior behind a single function call can be mind-bogglingly complex.

I recently returned to a Common Lisp program I have not touched for 9 years. It makes some use of the regex engine cl-ppcre (the > nine-year-old-version planted into the program), and I had to touch a few bits of code where I needed to introduce some new uses of that.

I had made a number of uses of a macro called register-groups-bind and just from looking at my own old uses of the macro, I was able to figure out how to use it again.

It is easier to use than most of the cl-ppcre API!

   (register-groups-bind (x y z)
                         ("regex..." input [options...])
     ;; here, x, y, z are bound to groups from the regex
    )

I needed to classify an input which could look like 123, 98% or 3/4. Integer, integer percentage or ratio. Easy:

  (register-groups-bind
    (num denom percent count)
     ("^(\\d+)/(\\d+)$|^(\\d+)%|^(\\d+)$" nq :sharedp t)
     ;; code here ...
  )

In the code, if count is true, we got the integer. If num is true, so is denom and we have the fraction. If percent is true, we got the percentage.

CL-PPCRE exposes the low-level objects: scanners you can create with create-scanner and use via the scan generic function. Using that would be a lot harder and verbose than the friendly macro.

My old code was easier to understand and maintain for me because of the easy macro.