3 ms·
In what ways is OCaml's pattern matching superior to Haskell's?
by jackpirate 11y ago
In what ways is OCaml's pattern matching superior to Haskell's?
- pcwalton 11y agoOr patterns. See http://stackoverflow.com/questions/24700762/or-patterns-in-haskell http://stackoverflow.com/questions/24700762/or-patterns-in-h...
- tikhonj 11y agoOn 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_guide/syntax-extns.html#pattern-synonyms https://downloads.haskell.org/~ghc/latest/docs/html/users_gu...
- barosl 11y agoI'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?
- dragonwriter 11y ago> 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/OrPatterns.html https://hackage.haskell.org/package/OrPatterns-0.1/docs/OrPa... [1] https://ghc.haskell.org/trac/ghc/ticket/3919 https://ghc.haskell.org/trac/ghc/ticket/3919
- marijn 11y agoI'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?).
- pcwalton 11y agoI'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.
- thoughtpolice 11y agoAs 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.
- gohrt 11y agoHow so? There's one case per branch. It's no more blowup than writing out the branches by hand would be.