I've just come across some Haskell code that looks something like this:
(functionOne, functionTwo)
| someCondition = (10, "Ten")
| otherwise = (20, "Twenty")
From the way the code is used I think I understand the intent of this code i.e. it is just a more concise way of writing this:
functionOne
| someCondition = 10
| otherwise = 20
functionTwo
| someCondition = "Ten"
| otherwise = "Twenty"
However, I can't recall ever seeing functions written this way before and have no idea what this technique is called so can't search for any additional information about this.
So my questions are:
These aren't functions, just variable bindings. You correctly understand how it works. It doesn't have any particular name, because it's just another application of pattern matching. Anytime you could declare a variable, you can declare a more complex pattern in that same position.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With