This is pattern matching . Link:
Functions may consist of one or more rules. A rule consists of its function name, argument template, and its expression. When the function is called, the values of the arguments will be matched with the patterns in order from top to bottom. Functions using pattern matching are very similar to case expressions.
This means that the channel separates cases for pattern matching. Matching a pattern matches special patterns and executes specific expressions based on that pattern. They follow the syntax:
fun <fun-name> <pattern> | <fun-name> <pattern> | <fun-name> <pattern>;
Where <pattern> :
<args> = <expressions>
In your first example, it declares a fac function for a factorial. The first pattern is when the int argument is 0. If int is 0, the expression of this case will be executed, and in this case, if the passed argument is 0, the result will be 1 (since 0 factorial is 1). If the argument passed is not 0, then it follows the following pattern (since it does not match the first pattern). If the argument passed is equal to, say, 2, it will do the recursion and find the factorial accordingly.
Consider the snippet below:
fun past "run" = "ran" | past "swim" = "swam" | past x = x ^ "ed";
We define a function called past that takes an argument and finds the elapsed time of the argument. The first template is equivalent - in plain English:
if the word (or argument passed) is running, the elapsed time is running.
The second template is equivalent:
if the word (or the argument passed) is “floating”, the elapsed time is “floating”.
If the word is not “swimming” or “starting” (and therefore the two patterns do not match), continue with the last pattern, which simply adds “ed” to the end of the word.
In this example, you can see that the characters | (pipe) share the individual patterns. You can think of patterns like this pseudocode:
if argument is "run", then passed tense is "ran" else if argument is "swim", then passed tense is "swam" else add "ed" to end of word
Pattern comparisons are used to cover occasional cases. Since running and swimming have irregular times, we cover these cases with patterns. The same principle applies to the first example - 0! is a special case, it is 1. Because of this, we can use pattern matching in accordance with a specific case, when the argument is 0 and is executed in accordance with this specific case.