Hacker News new | ask | show | jobs
by coliveira 594 days ago
I always thought that pattern matching would be an excellent feature for other programming languages, but it seems that it hasn't become popular. Many strategies available to Prolog could become possible just by the addition of this feature. One possible implementation of the idea occurs with C++ templates specialized with numerical parameters. It also seems that Mathematica provides pattern matching, but I don't use that language.
2 comments

Matching of patterns, with only a single occurrence allowed for each variable, is fairly popular in languages designed in the last two decades, isn’t it? A lot of those have or at least borrow from ML heritage, and that of course places a big emphasis on algebraic types and pattern matching. Full-blown unification remains niche, that’s true, but it’s just a fairly heavyweight feature, and I don’t really know how you’d integrate it without turning your language into (a superset of) Prolog.
Simon Peyton Jones (of Haskell) is working on Verse, a functional relational programming language, that merges the ideas of both functional and logical programming into one.

It’s being done to facilitate Fortnite metaverse, as he’s doing this work at Epic.

https://youtu.be/OJv8rFap0Nw?si=OmBT9CBJIxdJE1Jv

Functional logic programming much before Verse https://mercurylang.org/
Which interestingly enough is used to make PrinceXML [1] one of the only "browsers" to fully support CSS print media.

1. https://www.princexml.com

Erlang, Elixir, Swift, Rust, Python, probably some others.

That list is roughly in order of how capable and useful the pattern matching is in each of those languages. Erlang was originally written in Prolog.

Also, I definitely agree it's a feature I miss everywhere when not using Erlang/Elixir.

I mostly ignored Python until my job required it, about 8 years ago. Coming from Erlang, I was pleasantly astonished to find that I could use tuple pattern matching in function headers… until I discovered that was dropped in Python 3 and I had been using the mostly-dead version 2 runtime.
I think I'd call this destructuring, as opposed to patterns and matches, and Python still does it, just not in the function head.

I do wish they went the opposite direction and made the feature better instead of keeping it as a weird assignment thing in the body. The match statement is a similarly unfortunate solution, in my opinion.

Oh well. :)

You're correct, destructuring is the appropriate term.

I checked the release notes, and the reason they removed it: no one was using it. I guess it's unsurprising, but sad.

I looked at Erlang and it is closest to the Prolog idea (clearly because Erlang was originally implemented in Prolog). Other languages are not really what I mean by pattern matching, the matching should go on the selection of functions to apply to a particular parameter expression. In the compiled world, C++ is the one that gets closest to this behavior. Python would be able to do it due to its dynamic nature, but for some reason it decided not to implement this feature.
I'm curious, how does Swift have better pattern matching than Rust?
I agree with sulam, in that I think they're roughly equally nice to work with. Not as powerful as Erlang/Elixir, but more capable than Python.

There's a decent discussion from a few years ago on hn, as well: https://news.ycombinator.com/item?id=24662262

These are probably tied TBH.