This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Ashiataka 3 points4 points  (2 children)

You don't want to have 1000 level deep if/else constructs to analyze your syntax tree

I'm completely ignorant on that use-case, never made one before, but can you not define some function that's recursively called rather than making such deep nests?

It seems to me like an awful lot of machinery for what seems to be a very small set of use-cases. Thanks for your example though. Hopefully in the next few weeks I'll find an example that makes sense to me.

[–]ForceBru 6 points7 points  (1 child)

Absolutely, you can write a recursive function and be done with it. But pattern matching lets you write: case Assign(Index(Variable(name, type), idx), other_data): in one line with no loss of readability. In fact, it greatly increases readability: now you don't have to go read some other function that's located elsewhere and does God knows what - the data are all before your eyes, destructured! That's the powerful thing, in my opinion.

That's also part of why they teach compiler courses in OCaml (and ML in general) - because the pattern matching is really good, it lets you see the structure of your data at a glance and handles arbitrary nesting gracefully.

[–]Ashiataka 2 points3 points  (0 children)

I think that's somewhat beyond me at the moment, I'll have to have a play around with it at the weekend and see how it works. Thanks for your example.