I found a good description of higher order functions in “Eloquent Javascript”, of all places. It reads:

Functions that operate on other functions, either by taking them as arguments or by returning them, are called higher-order functions. If you have already accepted the fact that functions are regular values, there is nothing particularly remarkable about the fact that such functions exist. The term comes from mathematics, where the distinction between functions and other values is taken more seriously.

Higher-order functions allow us to abstract over actions, not just values.

I liked this description, it points out that functions are just values, and the use of higher-order function allows abstraction of functionality from data and actions. Higher-order functions is a way to achieve separation of concerns in your code.

Twice is an example of a simple higher-order function:

The following is the final part of the cheatsheet for the higher order functions in the List module. The previous articles were

### List.permute Example

`List.permute`

reorders a list using a function to decide where each element should go. The function is passed an index of an element, and returns a new index for the element. The function must return a unique and valid index for each element. If a repeat or an index that is out of range is returned then an `ArgumentException: The function did not compute a permutation`

is raised.

`List.permute : (int -> int) -> 'T list -> 'T list`

The example above might be a bit cryptic. It represents a combination lock which has three wheels of numbers. By spinning the wheels to the correct position, the lock is open when the numbers at the head of each wheels list matches the code. It makes use of the List.permute method to reorder the numbers as if somebody has rotated the wheel.

There is no direct LINQ equivalent for `List.permute`

.

### List.pick Example

`List.pick`

picks the first element from a list that satisfies the higher-order function. The list is iterated, calling the function with each item in turn. The function can return `Some`

value to pick it as the result, returning `None`

will continue the iteration through the elements. If the list is exhausted then an `KeyNotFoundException`

exception is raised. It is similar to `List.choose`

, except that `List.choose`

will return multiple items that match. `List.pick`

will turn the `option`

value it picks into a non-`option`

result. `List.pick : ('T -> 'U option) -> 'T list -> 'U`

There is no direct LINQ equivalent for `List.pick`

.

### List.reduce Example

`List.reduce`

reduces a list to a single value. It similar to `List.fold`

, however with `List.reduce`

you do not specify an initial accumulator. The initial accumulator value will always be the first element in the list. If `List.reduce`

is passed an empty list then an `ArgumentException`

is raised.

`List.reduce : ('T -> 'T -> 'T) -> 'T list -> 'T `

The LINQ equivalent to `List.reduce`

is the `Enumerable.Aggregate`

method.

### List.reduceBack Example

`List.reduceBack`

reduces a list to a single value. `List.reduceBack`

differs from `List.reduce`

in that it traverses the list backward. It is similar to `List.foldBack`

, however with `List.reduceBack`

you do not specify an initial accumulator. The initial accumulator value will always be the last element in the list, and because the list is iterated backwards this is processed first. If `List.reduceBack`

is passed an empty list then `ArgumentException`

is raised. `List.reduceBack : ('T -> 'T -> 'T) -> 'T list -> 'T`

### List.scan Example

`List.scan`

accumulates a result from a list of items rather like `List.fold`

. However, rather than as single accumulated result, it returns all the intermediate accumulated values followed by the final value as a list. `List.scan : ('State -> 'T -> 'State) -> 'State -> 'T list -> 'State list`

There is no direct LINQ equivalent for `List.scan`

.

### List.scanBack Example

`List.scanBack`

accumulates a result from a list of items rather like `List.foldBack`

. However, rather than as single accumulated result, `List.scanBack`

returns all the intermediate accumulated values. The reverse iteration of the list has the effect that the value at head of the result is the final value, where the tail elements work backwards to the first state.

`List.scanBack : ('T -> 'State -> 'State) -> 'T list -> 'State -> 'State list`

There is no direct LINQ equivalent for `List.scanBack`

.

### List.sortBy Example

`List.sortBy`

orders a list using a projection function. The projection function is called for each item in the list, and the return value is used as a key for ordering.

`List.sortBy : ('T -> 'Key) -> 'T list -> 'T list (requires comparison)`

The LINQ equivalent of `List.sortBy`

is `Enumerable.OrderBy`

.

### List.sortWith

`List.sortWith`

sorts a list using a sorting function. The sorting function is passed two elements from the list at a time and the logic and can return 1 where the first is greater than the second, -1 for the opposite and 0 for an equal result.

`List.sortWith : ('T -> 'T -> int) -> 'T list -> 'T list`

There is no direct LINQ equivalent for `List.sortWith`

.

### List.sumBy Example

List.SumBy adds up values returned by the projection function. The projection function is called for each item in the list and the logic can decompose the value and project a result as a return value for use in the summing calculation.

`List.sumBy : ('T -> ^U) -> 'T list -> ^U (requires ^U with static member (+) and ^U with static member Zero)`

The LINQ equivalent of List.SumBy is `Enumerable.Sum`

.

### List.tryFind Example

`List.tryFind`

finds an item in the list using a predicate function decompose and inspect the list elements. If the function returns `True`

, then `List.tryFind`

returns the item as an `option`

element. If the function returns `False`

, then the iteration of the elements continues until the list is exhausted. Then `None`

is returned.

`List.tryFind : ('T -> bool) -> 'T list -> 'T option`

There is no direct LINQ equivalent for `List.tryFind`

.

### List.tryFindIndex Example

`List.tryFindIndex`

finds the index of an item in the list using a predicate function to decompose and inspect the list elements. If the function returns `True`

, then `List.tryFindIndex`

returns the index of the item as an `option`

result. If the function returns `False`

, then the iteration of the elements continues until the list is exhausted. Then `None`

is returned.

`List.tryFindIndex : ('T -> bool) -> 'T list -> int option`

There is no direct LINQ equivalent for `List.tryFindIndex`

.

### List.tryPick Example

List.tryPick picks the first item in the list where `Some`

value is returned by the passed function. The result of `List.tryPick`

is `Some`

value or `None`

.

This function is similar to `List.tryFind`

which also returns the first item found, but `List.tryFind`

uses a predicate function instead of an `option`

function.

`List.tryPick : ('T -> 'U option) -> 'T list -> 'U option`

There is no direct LINQ equivalent for `List.tryPick`

.

Nick says

Thanks for this – the only explanation I could find of List.permute that explicitly sets out what the permutation function is doing. As it turns out it’s just what I need!