7 ms·
> Also, "if / else" only supports 2 cases, with pattern matching you can have an arbitrary amount of cases. Coming from a non-pattern marching language (python
by wodenokoto 6y ago
> Also, "if / else" only supports 2 cases, with pattern matching you can have an arbitrary amount of cases.
Coming from a non-pattern marching language (python) I’ve yet to see what the big deal is, maybe you can help.
I can have as many elif’s as I need cases and other than maybe not needing to repeat `x==...` I’m not sure what I’m missing out on.
With that being said, I do often wish for more elegant solutions to long winded if else
- bcheung 6y agoTo get an intuition for the expressive power you can look up examples of quicksort and factorial in languages that support pattern matching. Haskell is a good example. It is true that you can always just use if statements, it's more about the convenience and cleaner syntax. You can also do destructuring with pattern matching. Pattern matching is almost a requirement if you are going to use algebraic data types. As Paul Graham says in Beating the Averages, you have to look at the other language from the perspective of knowing it. Nobody can really explain it. It's just going to seem equivalent or weird syntax otherwise.
- wodenokoto 6y agoThank you for the thoughtful answer. It made me rethink the premise of my question.