Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In what ways is OCaml's pattern matching superior to Haskell's?



On the flipside, Haskell just got pattern synonyms[1] which are incredibly useful for being able to refactor and work with abstract types.

It also has view patterns which are quite useful. I don't know how to replicate that in OCaml.

Combining the two lets types expose fairly sophisticated interfaces as normal patterns, which is incredibly useful for things like graphs.

[1]: https://downloads.haskell.org/~ghc/latest/docs/html/users_gu...


I'm mildly surprised, why isn't this a thing in Haskell? Can't it be easily added, considering an or pattern is trivially expanded to two (or more) patterns?


> Can't it be easily added, considering an or pattern is trivially expanded to two (or more) patterns?

At least limited support has been done as a library providing a quasiquoter [0], and there is an open ticket for it as a ghc feature. [1]

[0] https://hackage.haskell.org/package/OrPatterns-0.1/docs/OrPa...

[1] https://ghc.haskell.org/trac/ghc/ticket/3919


I've been surprised/annoyed by this as well. My conclusion was that there are no practical problems that would make this hard, and it is most likely held back simply by the theoretical trivialness of it (what academic wants to work on something that doesn't make a good paper?).


I've heard that or-patterns can result in superlinear blowup in the number of cases, which is why Haskell compilers have not implemented it so far.


As far as I know, that's never really been a reason as to why we haven't implemented it in GHC. It's much more likely just that nobody has ever gotten around to implementing it. Shouldn't be especially hard, I imagine.


How so? There's one case per branch. It's no more blowup than writing out the branches by hand would be.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: