F # Lesson 4: Values, methods, and attributes in F #

Source: Internet
Author: User

1. Simple values: functions, parameters, and top-level items defined using let or pattern matching.

Examples: form, text, wordcount.

Simple value: functions, parameters and top-level member definitions are implemented using let or pattern matching.

Code

Let Form = New Form ()

Let Highlow a B =
Match (A, B) With
| ( " Lo " , LO ),( " Hi " , Hi) -> (Lo, hi)
| ( " Hi " , Hi ),( " Lo " , LO) -> (Lo, hi)

2. methods: function values associated with types. interfaces, classes, and record and Union types can all have associated methods. methods can be overloaded (see chapter 6) and must be applied immediately to their arguments.

Examples: system. net. webrequest. Create and resp. getresponsestream.

Method: type-related function value. Interfaces, classes, records, and Union types can all have related methods. Methods can be overloaded, and they must be assigned parameters.

3. properties: A shorthand for invoking method members that read or write underlying data. interfaces, classes, and record and Union types can all have associated properties. examples: system. datetime. now and form. topmost.

Attribute: a shortcut for calling a member function to read and write values. Interfaces, classes, records, and Union types can all have associated attributes. We can use the <-operator to assign values.

Code

Form. Text <-   " Form1 "

 

4. indexer properties: A property can take arguments, in which case it is an indexer property. indexer properties named item can be accessed using. [_] syntax. examples: vector. [3] and matrix. [3, 4].

Attribute indexer: an attribute with parameters. Here it is an attribute indexer. You can use. [_] to access the named property indexer.

 

In addition, we encountered a problem today, that is, how to use F # To sum the series. For example, if we enter a value of 100 and calculate the numbers from 1 to 100, if we use the C/C ++ language to write

Code

Int Accumulate ( Int N)
{
Int Result =   0 ;
For ( Int I = 0 ; I < N; I ++ )
{
Result = Result + I;
}

ReturnResult;
}

IfCodeTranslate directly into F #ProgramIt looks like this.

Code

Let Accumulate (N: Int ) =
Let   Mutable Result = 0
Let   Mutable I = N
While I> 0   Do  
Result <- Result + I
I <- I- 1
(Result)

However, mutable variables are not recommended in F #, so a better method is to use recursive functions.

Code

Let   REC Accumulate1 (N: Int ) =
If N <= 1   Then   1   Else N + accumulate1 (n- 1 )

As for the optimization method, I haven't thought of it yet.

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.