F #Lesson 2
I learned something again today.F #And have a new understanding of this emerging language. Now let's try it out.Vs2010 beta2AndF #The power.
First, we useVs2010Create a newF #Project.. NetThe same language.
Next we will begin to writeF #Program, SinceF #Yesfunction typeProgramming LanguageThen we will first write a simple function.
let charnum STR = let num = string. length STR num |
In the past, we need to import this fileF #. This process is a little troublesome. NowVs2010To help us solve this problem, you just needF #InVs ideShown in (View-> other Windows-> F # interactive). This is trueF #In the interactive window, all we need to do is to testCodeSelect, pressAlt + enterYou can import the function to the interaction environment.
This isIDEBenefits.F #There are two types of source files:*. FSAnd*. FSX.*. FSYesF #ImplementationSource codeFile, and*. FSXIsF #. The difference between them is that you can right-click*. FSXDirectly run the codeF #In the interaction environment.
SaidVs2010OfIDE, Let's talk about it later.F #Language. In the example above, friends who have learned other languages will find thisF #Language syntax rules are very special. A brief introduction to this example:
let charnum STR = /// declare a function let num = string. length STR /// declare a variable num /// return value |
KeywordsLet... =...YesF #The most common keyword. It can be used to declare variables, functions and objects. In additionF #Parameters are separated by spaces, rather than commas. The last line is the return value of the function. AllF #Each function has a return value, even if you do not write the return value,F #The compiler setsUnitThe type value is the return value. However, you may need to pay attention to this feature in cross-language calls.
Let's look at another one.ListExample:
LetList = [1. 10] LetList1 = List. Filter (FunN->N %2=0) List |
Running result
Val list: int list = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
Val list1: int list = [2; 4; 6; 8; 10]
This example contains 2 Items F # Features: one is a list container, and the other is Lambda Expression. In F # Declare and initialize List Containers are easy to use. [] . In fact F # There are three most common containers: Tuple (Tuples) ()Indicates, List (List) [] And Record (Record) {} . In fact, we initialize List More [X; y; Z] Method. For Lambda Expression. This is not F # Proprietary, Incorporated C # You can use Func <Int,Int> Template. The only difference is that F # A Fun Keyword. In both examples, I used F # Library functions String And List Class, which also shows F # You can also perform object-oriented development.
The last example today isF #MPs queue. We know that the role of a pipeline is to use the computing results of one party as the input of the other party. Improve the above example
[ 1 .. 10 ] |> list. filter ( Fun n -> N % 2 = 0 ) |
We can get the same result as the previous example.F #Pipeline in is used|>Therefore, the calculation result types on both sides of the MPs queue must be consistent.
These examples are justF #But if these examples are expressed in other languages, you need to do a lot of work. We can see thatF #Is very concise, andF #Its advantage lies in numerical calculation.
Digress:
On the Internet, many friends have encountered a compilation problem, that isString. SplitCompilation fails. I have studied this problem for a long time. ObviouslyVs2010 beta2Not integrated yetFsharp. powerpack. dllWe also need to manually reference this file in the interactive environment.