|
|
|
|
|
by kazinator
2178 days ago
|
|
A good pattern matching library in Lisp makes code a heck of a lot more readable. Firstly, even basic list destructuring with destructuring-bind is an improvement over a soup of car/cadar/caddr/. Suppose we are in a compiler and would like to look for expressions of he pattern: (not (and (not e0) (not e1) (not e2) ...))
in order to apply DeMorgan's and rewrite them to (or e0 e1 ...)
I would rather have a nice pattern matching case like this: (match-case expr
...
((not (and @(zeromore not @term)))
`(or ,term)) ;; rewrite done!
...)
than: (if (and (consp expr)
(eq (car expr) 'not))
(consp (cdr expr))
(consp (cadr (expr)))
(null (cddr expr))
(eq (caadr (expr)) 'and)
(eq (cadr expr)
... ad nauseum)
Even if a fail-safe version of destructuring-bind is used to validate and get the basic shape, it's still tedious: (destructuring-case expr
...
((a (b c))
(if (and (eq a 'and)
(eq b 'not))
... now check that c is a list of nothing but (not x) forms
)))
I don't have a pattern matcher in TXR Lisp. That is such a problem that it's holding up compiler work! Because having to write grotty code just to recognize patterns and pull out pieces is demotivating. It's not just demotivating as in "I don't feel like doing the gruntwork", but demotivating as in, "I don't want to saddle my project with the technical debt caused by cranking out that kind of code", which will have to be rewritten into pattern matching later. |
|