F # Adventure Tour (II): Functional Programming (Medium)

Source: Internet
Author: User
Tags connect

Operator (Operator)

In F #, you can think of an operator as a more elegant way of calling a function. There are two types of operators: prefix (prefix) and infix (infix), the former takes an operand (operand), occurs before the operand, and the latter accepts two or more operands, appearing between the first two operands.

F # provides a rich set of operators that can be used for numbers, Boolean values, strings, and collection types. These operators are very numerous, limited to space, this is no longer one by one detailed. This article will focus on how to use and define operators.

Operators like c#,f# can also be overloaded, that is, we can use different types for the same operator, such as "+", but unlike C #, each operand must be of the same type. F # 's operator overload rules are similar to C #, so any BCL or written in C #. NET class Library supports overloaded classes in the same way as in F #.

let words = "To live " + "is " + " to function."
open System
let oneYearLater = DateTime.Now + new TimeSpan(365, 0, 0, 0, 0)

We can define our own operators, or we can redefine any existing operators (not recommended). Take a look at the following bad practices:

let (+) a b = a – b
print_int (1 + 2)

See here, what do you think of? Is it clearly defined as a function? So we said earlier, "You can think of an operator as a more elegant way of calling a function." We redefined the "+" operator, so the result of "1 + 2" is-1, which is of course not good, how to change "+" back to its original meaning when using FSI in VS? I usually turn off the FSI process in the Task Manager, then press ENTER, "+" will come back.

The custom operator cannot contain letters and numbers, and the characters you can use are as follows:

!$%&+-./<=>?@^|~
:

The first character of the operator can be any character in the first line above, and the following character can be any character above. The definition syntax is similar to a function except that you want to enclose the action. Look at the following example:

let (+^*) a b = (a + b) * (a * b)

The result is 30.

List (Lists)

A list is a simple collection type that is placed in F #. Can be an empty table (empty list), using square brackets to represent ([]). We can connect a value to the list by using the "::" operator, and note that you want to use the value as the first operand:

let emptyList = []
let oneItem = "one" :: []
let twoItem = "two" :: oneItem

In VS, you can see that the Oneitem type is string list. If the list contains more than one item, the above method looks cumbersome, and we can use the following syntax:

let shortHand = ["hello "; "world!"]

In addition, we can use the "@" operator to connect two lists:

let concatenateLists = ["one, "; "two, "] @ ["three, "; "four"]

The element types in the F # requirements list must be the same, and if you really need the list to contain different types of elements, then you have to create a list of obj (that is, System.Object) type:

let objList = [box 1; box 2.0; box "three"]

The third box is optional, which reminds me of the boxing in C #.

Lists in F # are not modifiable and cannot be modified once they are created. Functions and operators that act on lists also cannot modify the list, but instead create a copy of the list. This feature is similar to the string type in C #. Look at the following example:

#light
Let printlist list =
List.iter print_string List
Print_newline ()
Let Threeitems = ["one";] Two ";" Three "]
Let reversedlist = List.rev threeitems
Printlist Threeitems
Printlist reversedlist

The ITER method above accepts two parameters, the first is a function, and the second is a list, which applies the function to each element of the list in turn, somewhat like a foreach loop in C #. The Rev method returns the list's inverse sequence table.

Printing results are:

one tow three
three two one

Neither of these methods has changed the original list. To learn more about the F # list, it is recommended that you read this article: Mastering F # Lists.

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.