What does channel (|) mean in ML programming?

For example, the following function:

fun fac (0 : int) : int = 1 | fac (n : int) : int = n * fac (n - 1) 

Or in the function:

 fun even 0 = true | even x = odd(x-1) and odd 0 = false | odd x = even(x-1); 

I have little experience with ML and am just trying to understand the basics.

+5
source share
2 answers

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.

+6
source

What you see is the shortcut syntax for defining a function and pattern matching at the same time. If you separate the template from the function definition, it will look something like this:

 fun fac (x : int) : int = case x of 0 => 1 | n => n * fac (n - 1) 
0
source

Source: https://habr.com/ru/post/1259131/


All Articles