3 ms·
In general, you can replace pattern matching with a function that takes one function for each constructor of the sum type. However, doing this kind of ... suck
by DarylZero 5y ago
In general, you can replace pattern matching with a function that takes one function for each constructor of the sum type.
However, doing this kind of ... sucks.
- eru 5y agoIt sucks when you have to use it for everything, but it's a useful enough technique that most Haskell datatypes come with functions that provide exactly this conversion to church encoding. See eg 'maybe' (https://www.stackage.org/haddock/lts-18.18/base-4.14.3.0/Prelude.html#v:maybe https://www.stackage.org/haddock/lts-18.18/base-4.14.3.0/Pre... on stackage) or foldr for lists. 'foldr' is interesting, because it encapsulates a recursive pattern matching on lists. For the non-recursive version, see 'uncons' composed with 'maybe'.
- DarylZero 5y agoIt's probably actually better than pattern matching when there are only two, as in those cases. But even at three, it becomes a mess. And more than three? Yuck.
- eru 5y agoIf your language does currying similar to Haskell, and there's a good order to your constructors / arguments, even with three or more there might be some benefits with partial application.