1. 15
  1. 3

    I always implement pattern matching first whenever I start using a new lisp. Haskell-style pattern matching is one of the few examples of a “good macro” I can think of.

    1. 1

      Funny; I write lisp all the time and I think pattern matching might have been the last really useful macro I wrote. (this was ~2 years ago)

      1. 4

        The trick with pattern matching is that it is necessarily syntactic in that it concerns itself with binding values to variables. Macros really only should be written for things which can be handled syntactically only. A good example where you don’t need macros is something like a prototype object system. Nothing about what is going on in such a system concerns syntax.

        With such an object system a set of syntactic extensions might be useful and you could write one on top of a regular old semantic implementation but this separation of concerns is impossible for a pattern matcher: it binds, therefor it is syntax.

        A related note: when I first learned Lisp (Common) I liked the sloppy macro system. Then I learned Scheme and liked hygienic macros but was comforted by syntax-case’s ability to break hygiene. Then I gradually came to think that breaking hygiene almost never makes sense and then I started to think that writing macros almost never makes sense. Scheme is still my favorite language but I’m deeply suspicious of syntax transformation for almost any reason except things like pattern matchers.

        1. 4

          Would you mind sharing that macro? :)

          1. 3

            Sure; here’s the initial version of it: https://p.hagelb.org/match.fnl.html

            The latest version is a bit harder to follow since we added a new syntax for guard patterns: https://git.sr.ht/~technomancy/fennel/tree/main/item/src/fennel/macros.fnl#L311

            TBH if I saw a macro of this complexity added to our codebase at work I would immediately flag it during code review. But in this case it’s built into the language itself, which changes the dynamic because everyone learning the language will need to learn it. Because pattern matching is SO fundamental to programming, I believe it’s justified.