by p-e-w · 4y ago · 0 comments
Not deliberately obfuscated, true. But any code that contains tokens like "CAR", "caar", "CDR", "cdar", "CONS", "caddr", etc doesn't belong in a modern production environment where auditability and maintainability are paramount. What's the goal here, shortest possible text? Why not just replace each of those with a Unicode Greek letter and save two bytes and three characters each?
If that means Lisp itself is the problem, so be it. The assumption that "software engineers" in the late 1950s somehow designed the holy grail of programming languages is laughable anyway.
Do you code in Lisp? I do and I don't find it obfuscated at all. On the other hand I find it absolutely clear and beautiful.
car, caar, cdr, etc. are no more obfuscated than chmod, chown, cp, ls, etc. in shell. If you are unfamiliar with Lisp, sure they would seem obfuscated but so would those commands in shell to someone who has never done shell. But after using the shell for a while those commands become clear, concise and convenient. So do car, cdr, caar, etc. "cons" just constructs a cell. (cons "foo" "bar") constructs a cell with "foo" as its car (first element) and "bar" as its cdr (second element). It can't get simpler than that!
> It can't get simpler than that!
It can, because in your description you introduce multiple levels of indirection.
What would improve things would be to have the mirror image functions:
rac
rdc
raac
rdac
radc
rddc
and so on. We might as well drop the final c, too.These would read out the traversal from left to right. (radad obj) would take the car, then cdr, then car, then cdr, left to right.
This would be better in a left-to-right threading macro:
(--> (whatever) radad (+ 3) ...)
because it would be equivalent to (--> (whatever) car cdr car cdr (+ 3) ...)
the expansion to car/cdr is in the same order.The accessor functions like caddr should probably be avoided in code which is using cons cells as linear lists. If someone is writing a server that pops requests from a list, and I see caddr in that code, I will probably flag that in the review.
The functions are useful in code that manipulates nested cons cell structure, and it's important to be familiar with them.
I'd prefer destructuring or pattern matching to be used.
Some Lisp dialects don't have a pattern matching library included; it requires third party code. Destructuring can be cumbersome because it has a variable in every position of the pattern.
Say we have a variable called obj which holds something like (1 ((2 3)) ...) and we are interested in extracting the 3, and don't care about the other values. Destructuring can be cumbersome; e.g.
(destructuring-bind (a ((b c)) &rest r) obj
c) ;; a, b and r are ignored
Here, there may be excellent justification in just doing (cadaadr obj), if you happen to have the function to that depth. You likely don't, so a break-up into two is required such as (cadar (cadr obj)) and that may even be more readable, since "cadar" and "caddr" are recognizable idioms.In some lower level parts of a Lisp implementation, you may not have any pattern matching or destructuring available, due to the bootstrapping chicken-and-egg problem: the code is helping to implement that.
The meta-circular interpreter is like that. It could use destructuring, but then the goal is for the meta-circular interpreter to be able to handle all the constructs that are used in its own code; thus a definition of destructuring would be required, making it a lot larger.