3 ms·
For those who have never used a functional programming language, those often allow you to do "if-less" or at the very least "if-lite" programming via pattern ma
by davidw 14y ago
For those who have never used a functional programming language, those often allow you to do "if-less" or at the very least "if-lite" programming via pattern matching.
- ufo 14y agoThe thing with pattern matching is that it is as safe as the OO approach, in the sense that the compiler will warn you if you don't match all the cases and that forcing you to use an explicit ADT protects against boolean blindness (for example, writing code that assumes you are in case 2 if you are not in case 1) That said, when it comes to extensibility, pattern matching is more similar to if statements then the OO - its easy to write new functions but hard to extend the original ADT with new cases. If being able to add new functions is important it might be better to use if statements then to do a major rewrite to use OO instead. (You could always use a visitor pattern if you want the extra compiler safety but that can get very complicated, IMO)
- CJefferson 14y agoIs pattern matching really that different from switch statements, which are really just fancy if statements? Trying not to sound sarcastic, but if people are for if-free programming, pattern matching does not seem to be the answer for me. When I add a new type in haskell, I usually find myself having to look through all my pattern matchings.
- riffraff 14y agoI second the question, pattern matching is cool for destructuring or for completeness checking (not sure those are fundamental properties of pattern matching though), but it does not solve the problem of adding new behaviour without changing existing code any more than an `if` does.
- ufo 14y agoThis is not a bug - its a feature! Pattern matching/ifs are good for creating new functions, while OO style makes it hard to add new methods.
- danieldk 14y agoIs pattern matching really that different from switch statements, which are really just fancy of statements? Mostly, yes. Pattern matching also provides destructuring, allowing you to bind constructor arguments and pattern match against such arguments as well. For instance: fun (Just (x:_)) = ... But some of the downsides are comparable to switch statements, e.g. if you modify: data MyType = Foo | Bar to data MyType = Foo | Bar | Baz You will have to (potentially) update all functions or case expressions to account for Baz. One could use parametric polymorphism, comparably to the linked article, to make more extensible code. In such a case, one would define a type class such as: class (Show a) => Printer p where printIt :: p -> a -> IO () And one could make particular printers of this typeclass. You could even throw in existential quantification so that a function does not specialize to a particular Printer.
- ulrikrasmussen 14y agoPattern matching is a lot more expressive that switch and if statements, as it combines testing with elimination. For instance, let's say we want to write a new tail function, which returns the tail of a list but returns [] when the list is empty (pseudo-Haskell): tail2 xs = case xs of { Nil => []; Cons(x,xs) => xs } tail2' xs = if xs == Nil then [] else tail xs In the second case, tail2', the compiler won't stop us if we switch the two branches. In the first case, tail2, we only get access to the tail of the list if the list is actually non-empty. In essence, the difference is that if statements throws away any static information about the test result, whereas pattern matching constructs lets that information flow to each branch through variable binding.