F # Lesson 3:. Net Library and F #

Source: Internet
Author: User

Continue to study F # today. The main content is about the reference of the. Net Library in F.

First, we introduce some common. Net namespaces to the F # environment.
Open System
Open System. net
Open System. Io
Open System. Text
Open System. xml
Open System. Windows
Open System. Windows. Forms

 

In the first example, we will first show the strong compatibility of F # To obtain the HTML of a website.Code
Let website = "http://www.google.com /"

Let gethtmlbyurl (URL: string) = // note that httpwebrequest is used. the create method has two overload functions. Therefore, we need to explicitly specify the parameter type.
Let request = httpwebrequest. Create (URL)
Let response = request. getresponse ()
Let reader = response. getresponsestream ()
Let strreader = new streamreader (Reader)
Let reshtml = strreader. readtoend ()
Reshtml

Run this code and we will get the following output:

> Gethtmlbyurl (website );;
Val IT: String =
"<! Doctype HTML>

This code is too long to be stuck. In this example, the. NET web class is used to obtainSource code, We can see that F # is very convenient when using the. NET library. You only need to use the open keyword to open the required. Net package. This keyword is similar to using in C # and import in Java. In this example, we also use classes such as streamreader to help us get the code content.

 

In the following example, we try to output the source code obtained above to a winform window.
Let mainform = new form ()
Let textbox1 = new Textbox ()
Textbox1.dock <-dockstyle. Fill
Textbox1.multiline <-true
Textbox1.text <-gethtmlbyurl (website)
Mainform. Controls. Add (textbox1)
Mainform. showdialog ()

Output:


In this example, a winform object is created, a textbox object is created, some attributes are set, and the obtained HTML source code is displayed in this text box. Basically, operations are the same as the C # code. The only thing you need to note is that the value assignment operator of F # is <-, rather than our common = sign. This assignment method is very common in the field of mathematics. Therefore, at the beginning of programming languages, mathematicians are full of objections to the use of = in programs.
From the two examples above, we can see that F # is very compatible with the. NET library, which also improves the practical value of this language.

 

In the last example, we will go back to the frequently used XML document development. Let's take a look at how we develop XML in F #.
Let xml = "<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?>
<Root>
<Child> 1 </child>
<Child> 2 </child>
<Child>
<Subchild> 3 </subchild>
</Child>
</Root>"

Let Doc = new xmldocument ()
Doc. loadxml (XML)

We traverse the XML nodes one by one and put the content of the nodes into a list.
Let items =
[
For node in Doc. selectnodes ("root/child") Do
Let link = If node. firstchild. nodetype = xmlnodetype. Text then node. innerxml else node. firstchild. innertext
Yield (Link)
]
How about the code!

 

Send two complaints:

Vs2010 beta2 ide does not support native C/C ++ very well. Even the memory window is not integrated. You cannot set the targetProgramPut it in the project directory for debugging. It seems that you have to wait.

 

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.