F # Tutorials: higher-order functions

Source: Internet
Author: User
Tags filter

A higher order function is a function that takes a function as an input parameter or a return value. It is difficult to understand from the name, but from the C # Point of view is to pass in or return to delegate and so on.

Before we can define higher-order functions ourselves, we should learn to use higher-order functions.

A number of higher-order functions are defined in the list, and this is the time to learn a few of them. First try the find function.

let list = [15; 7; 8; 3; 6; 10]
let even n = n % 2 = 0 
let x = List.find even list
printfn "%A" x

Where the first parameter of find is a function, and the second parameter is the Passed-in list. The function specified by the first parameter acts on each element of the list, and if the result is true, the element is returned as a value. This example is looking for the first even number.

The result of the run is show 8. It's a bit like the list.find approach to the. NET Framework.

But it is also possible to write this:

let list = [15; 7; 8; 3; 6; 10]
let x = List.find    (fun n -> n % 2 = 0) list
printfn "%A" x

The lambda expression is used as the input parameter for find. I think the wording should be used frequently.

Then we look at the exists function. The EXISTS function is used to determine if there are elements in the list that match the conditions specified in the input function, and return True if there is one.

let list = [15; 7; 8; 3; 6; 10]
let even n = n % 2 = 0 
let x = List.exists even list
printfn "%A" x

Then we try to use the next map function. The map function allows incoming functions to be used separately for each element of the list, and the computed results form a new list return.

let list = [15; 7; 8; 3; 6; 10]
let x = List.map (fun n -> n % 2 = 0) list
printfn "%A" x

The results of the operation are:

[false; false; true; false; true; true]

Does it feel like a LINQ select method?

Perhaps some people may not understand higher-order functions. But if you can understand the lambda, it's not difficult to understand it.

We continue to understand the other functions provided by list. First look at SortBy. The code sorted by string length is as follows:

let list = ["oracle"; "microsoft"; "ibm"; "google" ]
let x = List.sortBy (fun n -> n.Length) list
printfn "%A" x

However, there is a compilation error at length because there is no way to tell if n has a length attribute. After knowing this, it's easy to revise:

let list = ["oracle"; "microsoft"; "ibm"; "google" ]
let x = List.sortBy (fun (n:string) -> n.Length) list
printfn "%A" x

where the type of n is displayed as String.

Then we look at the filter function. This function is used to filter out elements that correspond to the conditions specified by the lambda expression.

let list = [1;2;3;4;5]
let x = List.filter (fun n -> n % 2 = 0) list
printfn "%A" x

This is similar to the LINQ where method. The result of the operation is [2; 4].

The ITER function corresponds to the C # foreach method.

let list = [1;2;3;4;5]
let x = List.iter (fun n -> printfn "%A" n) list

The result of this code is to display 1 to 5 sequentially.

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.