It’s slightly less weird when you consider that operators in Haskell are just syntactic sugar for functions, and functions can be partially defined using pattern-matching on the LHS of the definition. For example, this trivial function returns True when its argument is 0, and False otherwise. You could of course trivially define it in one body too, but you can also use pattern-matching on arguments like this:
isZero 0 = True
isZero _ = False
So you can partially shadow a function locally by defining it with a pattern-matching head: anything that matches the local definition will execute that, and anything that doesn’t continues to search bindings further out in scope.
Another fun one:
(Plus a very stern non-exhaustiveness warning.)
Is that a redefinition of infix + defined only on a left and right argument of 2? Terrifying.
It’s slightly less weird when you consider that operators in Haskell are just syntactic sugar for functions, and functions can be partially defined using pattern-matching on the LHS of the definition. For example, this trivial function returns
Truewhen its argument is 0, andFalseotherwise. You could of course trivially define it in one body too, but you can also use pattern-matching on arguments like this:So you can partially shadow a function locally by defining it with a pattern-matching head: anything that matches the local definition will execute that, and anything that doesn’t continues to search bindings further out in scope.
Yep! That’s exactly it. :)