F # ≥c # (pattern matching)

Source: Internet
Author: User

In this article, we will talk about pattern matching of F #. pattern matching plays an important role in F #. msdn describes it as follows: "Mode" is a rule used to convert input data. The mode will be used in the entire F # language. It compares data with one or more logical structures in multiple ways, splits the data into various components, or extracts information from the data. In some cases, it is quite similar to the switch in C #, but in F #, pattern matching is much more powerful than switch. Okay, next, let's enjoy this tour of F ~ There are not many contents, but they are brilliant and easy to understand ~

Before introducing it, let's take a look at the following code:

let a = 1       //nothing fancylet b = 1, 2  // b is tuple now

B is defined as a tuples. We have talked about the concept of tuples before and compared it with swap in C #. If you are interested, click here to view details. Back to this article, in the code above, if you want to get the first and second elements of a tuples for friends who are used to C # development, the most obvious method is to call the FST and SND functions, which is of course a method, but now we can use pattern matching to achieve the same effect:

let c,d = b   // c = 1 and d = 2

Next, we will try to use pattern matching to break down a list:

let l = [1;2;3;4]match l with| h::t -> ...     // h is 1 and t is [2;3;4]| _ -> ...

Note: (cons) operator has two functions: the first is to attach elements to the list, the second is to separate the list header from the list end, and the second function is used here. In the code above, we can see that the header of a list is separated from the end of the list, so we get the expected result, if you want to get 1, 2, 3, and 3, what should you do? It is also very simple. See the following code:

match l with| h0::h1::t -> ... // h0 is 1, h1 is 2, and t is [3;4]| _ -> ...

If the list length is known, for example, the list contains only four elements, you can use the following method to break down the list:

match l with| [a;b;c;d] ->     //a=1, b=2, c=3, and d=4| _ -> ...

Now, we have finished introducing pattern matching today. Are there any gains? At the same time, I am also curious about how to use C # to implement the same function. :)

Note: This article is a translation, if you are interested, please refer to the original article of the blogger Blogspot: http://apollo13cn.blogspot.com/2012/11/f-c-pattern-matching.html

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.