4 ms·
w.r.t. x::xs vs. head::tail One of the nice conceptual things about pattern matching is the pattern on the left hand side describes which bits of the structure
by ajanuary 14y ago
w.r.t. x::xs vs. head::tail
One of the nice conceptual things about pattern matching is the pattern on the left hand side describes which bits of the structure you're interested in, while the stuff on the right hand side describes how to manipulate that data.
When you're looking at the rhs it shouldn't care about where data came from, it should just care about what the data is. I've got some arbitrary thing `x` and a list of arbitrary things `xs`. I've got some `person` and a list of `people`.
Using `head` and `tail` makes the rhs concerned with the structure of the incoming data. This becomes more of a problem if you have more complicated data structures on the lhs.
One of the key elements of readable code is how easy it is to map your conceptual model onto source code onto executable code. Using head::tail wouldn't help new people grok the separation of structure on the lhs and manipulation on the rhs.
All that said, it's such a minor point as to probably not really have any impact in this case. Just a bit of silly purist hand-waving.
- ufo 14y agoI really like this analogy. I think a good example of it is the following code for red-back trees: balance :: Color -> RBTree a -> a -> RBTree a -> RBTree a balance B (Fork R (Fork R a x b) y c) z d = Fork R (Fork B a x b) y (Fork B c z d) balance B (Fork R a x (Fork R b y c)) z d = Fork R (Fork B a x b) y (Fork B c z d) balance B a x (Fork R b y (Fork R c z d)) = Fork R (Fork B a x b) y (Fork B c z d) balance B a x (Fork R (Fork R b y c) z d) = Fork R (Fork B a x b) y (Fork B c z d) balance k a x b = Fork k a x b The lhs does the dirty work of identifying the relevant bits in the tree and in the rhs things get rebalanced in a similar form. (Another thing I find amusing in this example is that its one of the few examples of Haskell code where you can't get around the "copypasting" by using `let` or `where`)