Post 7 – Pattern Matching & Recursion

Tram Ho

After going through the tools that support manipulating basic data types, we continue to look to the group of tools that support creating flexible processing logic for the code depending on the state of the received data and Iterate operations on the data set. And in this article, we will need to test the code of the sub-program , so interacting with the resulting code we will do with elm reactor .

http://localhost:8000/src/

Pattern Matching

The first is a logical construct called Pattern Matching , which is often seen as equivalent to the conditional or branching structure in Imperative environments. This name has two words and we will take care of the Matching element first. Purely Declarative languages ​​call the branching logic structure Matching because our program will not have sequential statements but parallel definitions instead.

The above definition of day n will be read by the compiler in turn as:

Particularly for the last position, the _ symbol will be read as other values ​​of n . Now we will modify the main program and see the results of running the Tell.day code in the browser.

http://localhost:8000/src/Main.elm

Ok. so we have a switch..case syntax that works fine. However, you see, the difference here is that, on the right side of the case are value values, not statement statement . The case..of syntax we see here, is a form of relative expression that replaces the repeated rewriting of the name of the sub-program in the definition as explained above. Therefore, people use the word Matching instead of the word conditional in the Imperative environment.

What about Pattern ?

That word means format – i.e. we will be able to match by the formats of the data, not necessarily specific values. For example, the type name of the received value, or the format that describes the states of the data structures – for example empty List , 1 element, 2 elements, or more, etc..

http://localhost:8000/src/Main.elm

In this case, if we pass in Just.any a Just containing any value, the displayed result is also "Something" . If you pass Tell.any Nothing , the display result will be the string "Nothing" . Does that mean that the Tell.any only interested in what sort of value the value is in Maybe , not the question of what the value is? Quantitative how much? Or what’s the content?

Let’s continue to write a subroutine to determine the data state of any List .

In the example code, the pattern in the last case is x::xs which means when List l is the result of inserting an element x into a List xs containing other values ​​similar to x . It also means when List l contains at least 2 or more elements – including X and other X .

Is it possible to match at the same matching with specific values ​​set in pattern ?

Yes, for sure! If there are a finite number of special values ​​of interest in the processing logic of the code, we can put those values ​​in place of the variables used in the pattern . Now Elm will check the appropriateness of the actual data both in terms of pattern and value at the location of variables.

The pattern for such List are already quite flexible. Now let’s try as an example with Tuple describing the coordinates of points in 3D space –

http://localhost:8000/src/Main.elm

Recursion

After knowing how to create dynamic logic for the code based on the type and format of the data, we need another tool to support iterative operations on the List dataset. We already know how to use this tool before. The definition of recursion was mentioned in the Imperative & Declarative article of the Self-Taught Web Programming Series.

http://localhost:8000/src/Main.elm

Uh… now that I’m writing this, I’ve noticed that we need a solution to shorten the sub-program calls that stack multiple layers like that. The parentheses placed on the same line of text will be quite difficult to recognize quickly when we skim the code.

Simply, for every pair of parentheses, we will replace it with a <| . symbol to pass the result of the inner sub-program to the outside on the left side. You can also use the |> symbols to arrange the order of the sub-program in reverse or the vertical list is also very easy to follow. However, such a sequence of writing will make our logical reasoning pattern inclined towards Imperative and will definitely affect the familiarity of thinking about solving problems in a recursion way, which is especially important in this environment. Declarative field.

Everyone’s choice will probably be different. I choose to keep the order of writing the names of the sub-program recursively. And when writing a vertical list, each symbol <| will be able to understand that the result of code execution will be returned to the above line.

Recursion in JS

[Declarative Programming + Elm] Lesson 8 – Conditional Expression

Share the news now

Source : Viblo