Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the F# data type Discriminator Union

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to understand the F# data type Discriminator Union? for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Digression:

I wrote this mainly in the hope that more .net developers can understand Franks and not be confused by a bunch of strange symbols when they see the F# code (there are actually few strange symbols). I didn't say that F# is better than other languages and will replace C#. I just hope that more people will understand and start using F# (C# is used more often, and it's good to know about F# to change your brain). The examples and codes are relatively simple. I hope you can forgive me a lot. Some friends may not have VSTS 2010 Beta1 on hand, but it doesn't matter, because F# also has a stand-alone installation package for VSTS 2008, which you can download and install here.

Brief introduction to Discriminator Union of F# data type

In the last section, we learned how to declare variables and define functions in F# through a simple example, and used two important data types List and Array in F#. Today I mainly introduce a very important immutable data type Discriminated Unions in F#. First of all, let's look at an example, which is a simple example of generating a binary search tree.

Type Tree | Nil let generateBinarySearchTree l = let rec insert a = function | Node (root,left,right) when a

< root ->

Node (root, (insert a left), right) | Node (root,left,right) when a > root-> Node (root,left, (insert a right)) | Nil-> Node (a, Nil) Nil) let rec loop acc = function | []-> acc | hd::tl-> loop (insert hd acc) tl loop Nil l let tree1 = generateBinarySearchTree [5 3, 9, 4, 6, 7]

Let's first take a look at the first three lines. Yes, this is the F# data type that we are going to focus on today: Discriminator Union.

Type Tree | Nil

First of all, notice that we are using type this time instead of the let keyword commonly used earlier. F# uses the type keyword to define user-defined types, here we define a type Tree, then the following Tree

It means that the type of Node is'a * Tree, so what does this mean? in fact, it is another important type of immutable in F#, Tuple, Tuple is easy to understand, it means to treat a set of data logically as a whole. Just look at an example (note that the delimiter is a comma)

Let S2 = (1, "hello")

Here we define a Tuple of type int * string. To use the values in it is also easy, we can declare new variables and initialize them with the value of s. Let iMagol s = S2 means that we have declared the int variable I, whose value is 1, and the string variable s, which has a value of 2)

Going back to our example,'a * Tree is easy to understand because you can reference yourself recursively when defining a Discriminated union.

The second case of Tree, Nil, is simple. It represents an empty node with nothing.

Through my detailed explanation above, I think you also understand what Discriminated union is, which represents a limited set of alternative scenarios, and each case has its own strict definition. Going back to our example above, Tree has two situations, either a'a * Tree Node or an empty Nil. You can see that it is often used in combination with Pattern matching, so you can see why it is called Discriminated union.

If you read the previous article carefully, the code for building a binary search tree is relatively simple, so I won't explain it. Let's next look at how to determine whether a value is in a built binary search tree.

Let rec tryFind x = function | Node (root,_,_) when x = root-> Some (x) | Node (root,left,_) when x tryFind x left | Node (root,_,right) when x > root-> tryFind x right | _-> None

First of all, notice that I used five'_', and the first four look a little different from the one. Remember I said in the previous article that'_'is used in Pattern Matching to match all other situations, and I said that Pattern Matching in F# is more powerful than Switch in C#, so we can see its power here. After finding a match, it can bind a variable name for each part of the match to facilitate our later invocation, if we are only interested in certain parts during binding. Then we can use'_ 'instead of the parts we are not interested in (note that' _ 'can only bind one corresponding part, and to correspond to two, we have to type two' _','_').

Secondly, we noticed that the return value of tryFind seems to have two cases, Some (x) and None. How can a function return two different types of values? Oh, forget that we are mainly talking about Discriminated union today? This is a pre-defined discriminated union in F#, it has its own name Option, its definition is very simple, with the previous basis, this does not need me to explain it.

Type option

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report