Calculemus


Search Module

Searching functions.

Functions and values

Function or value Description

mapfilter f l

Full Usage: mapfilter f l

Parameters:
    f : 'a -> 'b - The input function.
    l : 'a list - The input list.

Returns: 'b list The list of successful application's results.

Applies a function to every element of a list, returning a list of results for those elements for which application succeeds.

f : 'a -> 'b

The input function.

l : 'a list

The input list.

Returns: 'b list

The list of successful application's results.

Example

 [1;2;3;4] 
 |> mapfilter (fun n -> if n % 2 = 0 then string n else failwith "f")
Multiple items
val string: value: 'T -> string

--------------------
type string = System.String
val failwith: message: string -> 'T
Evaluates to ["2"; "4"].

Example

 [1;2;3] 
 |> mapfilter (fun n -> if n > 3 then string n else failwith "f")
Multiple items
val string: value: 'T -> string

--------------------
type string = System.String
val failwith: message: string -> 'T
Evaluates to [].

maximize f l

Full Usage: maximize f l

Parameters:
    f : 'a -> 'b - The input function.
    l : 'a list - The input list.

Returns: 'a The element of the list that maximises the function.

Finds the element of a list l that maximizes a function f.

f : 'a -> 'b

The input function.

l : 'a list

The input list.

Returns: 'a

The element of the list that maximises the function.

Example

 maximize (( * ) -1) [-1;2;3]
Evaluates to -1.

minimize f l

Full Usage: minimize f l

Parameters:
    f : 'a -> 'b - The input function.
    l : 'a list - The input list.

Returns: 'a The element of the list that minimizes the function.

Finds the element of a list l that minimizes a function f.

f : 'a -> 'b

The input function.

l : 'a list

The input list.

Returns: 'a

The element of the list that minimizes the function.

Example

 minimize (( * ) -1) [-1;2;3]
Evaluates to 3.

optimize ord f l

Full Usage: optimize ord f l

Parameters:
    ord : 'a -> 'a -> bool - The order that defines whether to maximize or minimize.
    f : 'b -> 'a - The input function.
    l : 'b list - The input list.

Returns: 'b The element of the list that optimizes the function.

Finds the element of a list l that maximizes or minimizes (based on the given ord) a function f.

Used to define maximize and minimize.

ord : 'a -> 'a -> bool

The order that defines whether to maximize or minimize.

f : 'b -> 'a

The input function.

l : 'b list

The input list.

Returns: 'b

The element of the list that optimizes the function.

Example

 optimize (<) (( * ) -1) [-1;2;3]
Evaluates to -1.

Example

 optimize (>) (( * ) -1) [-1;2;3]
Evaluates to 3.

tryfind f l

Full Usage: tryfind f l

Parameters:
    f : 'a -> 'b - The input function.
    l : 'a list - The input list.

Returns: 'b The first successful application of the function to an element of the list.

Returns the result of the first successful application of a function to the elements of a list.

tryfind f [x1;...;xn] returns f xi for the first xi in the list for which application of f succeeds.

Fails with tryfind if the application of the function fails for all elements in the list. This will always be the case if the list is empty.

f : 'a -> 'b

The input function.

l : 'a list

The input list.

Returns: 'b

The first successful application of the function to an element of the list.

Exception Thrown with message tryfind when the application of the function fails for all elements in the list.
Note

Perhaps it could be replaced with the standard ListModule.TryPick.

Example

 [1;2;3] 
 |> tryfind (fun n -> if n % 2 = 0 then string n else failwith "f")
Multiple items
val string: value: 'T -> string

--------------------
type string = System.String
val failwith: message: string -> 'T
Evaluates to "2".

Example

 [1;2;3] 
 |> tryfind (fun n -> if n > 3 then string n else failwith "f")
Multiple items
val string: value: 'T -> string

--------------------
type string = System.String
val failwith: message: string -> 'T
Throws System.Exception: tryfind.