Calculemus


Function Module

Functions over predicates and functions.

Functions and values

Function or value Description

can f x

Full Usage: can f x

Parameters:
    f : 'a -> 'b - The function to test.
    x : 'a - The argument on which to test the function.

Returns: bool true, if f x succeeds; false, otherwise.

Tests if an input function f can be applied to an argument x without failing.

f : 'a -> 'b

The function to test.

x : 'a

The argument on which to test the function.

Returns: bool

true, if f x succeeds; false, otherwise.

Example

 can List.head [1;2]
Multiple items
module List from Microsoft.FSharp.Collections

--------------------
type List<'T> = | op_Nil | op_ColonColon of Head: 'T * Tail: 'T list interface IReadOnlyList<'T> interface IReadOnlyCollection<'T> interface IEnumerable interface IEnumerable<'T> member GetReverseIndex: rank: int * offset: int -> int member GetSlice: startIndex: int option * endIndex: int option -> 'T list static member Cons: head: 'T * tail: 'T list -> 'T list member Head: 'T member IsEmpty: bool member Item: index: int -> 'T with get ...
val head: list: 'T list -> 'T
Evaluates to true.

Example

 can List.head []
Multiple items
module List from Microsoft.FSharp.Collections

--------------------
type List<'T> = | op_Nil | op_ColonColon of Head: 'T * Tail: 'T list interface IReadOnlyList<'T> interface IReadOnlyCollection<'T> interface IEnumerable interface IEnumerable<'T> member GetReverseIndex: rank: int * offset: int -> int member GetSlice: startIndex: int option * endIndex: int option -> 'T list static member Cons: head: 'T * tail: 'T list -> 'T list member Head: 'T member IsEmpty: bool member Item: index: int -> 'T with get ...
val head: list: 'T list -> 'T
Evaluates to false.

check p x

Full Usage: check p x

Parameters:
    p : 'a -> bool - The input predicate.
    x : 'a - The element that should satisfy the predicate.

Returns: 'a The input element x itself, if it satisfies p.

Checks if the value x satisfies the predicate p.

p : 'a -> bool

The input predicate.

x : 'a

The element that should satisfy the predicate.

Returns: 'a

The input element x itself, if it satisfies p.

Exception Thrown with message check, when x does not satisfy p.
Example

 check ((=) 2) 2
Evaluates to true.

Example

 check ((=) 2) 4
Throws System.Exception: check.

funpow n f x

Full Usage: funpow n f x

Parameters:
    n : int - The number of times to apply the function.
    f : 'a -> 'a - The function to apply.
    x : 'a - The element to apply the function to.

Returns: 'a The result of applying f to x for n times, if n is >= 0. Otherwise, the input argument x unchanged.

Iterates the application of a function f to an argument x a fixed number n of times.

funpow n f x applies f to x for n times, giving the result f (f ... (f x)...) where the number of f's is n. funpow 0 f x returns x. If n is negative, it is treated as 0. It fails, if any of the n applications of f fail.

n : int

The number of times to apply the function.

f : 'a -> 'a

The function to apply.

x : 'a

The element to apply the function to.

Returns: 'a

The result of applying f to x for n times, if n is >= 0. Otherwise, the input argument x unchanged.

Example

 2. |> funpow 2 (fun x -> x ** 2.)
Evaluates to 16.

Example

 2. |> funpow 0 (fun x -> x ** 2.)
Evaluates to 2.

Example

 2. |> funpow -1 (fun x -> x ** 2.)
Evaluates to 2.

Example

 2. |> funpow 2 ((/) 0)
Throws System.DivideByZeroException: Attempted to divide by zero.

non p x

Full Usage: non p x

Parameters:
    p : 'a -> bool - The predicate to reverse.
    x : 'a - The element to apply the inverse to.

Returns: bool true if p x returns false, otherwise false.
Modifiers: inline
Type parameters: 'a

Applies the inverse of a predicate p to an argument x.

p : 'a -> bool

The predicate to reverse.

x : 'a

The element to apply the inverse to.

Returns: bool

true if p x returns false, otherwise false.

Example

 4 |> non ((=) 2)
Evaluates to true.

Example

 2 |> non ((=) 2)
Evaluates to false.

repeat f x

Full Usage: repeat f x

Parameters:
    f : 'a -> 'a - The function to apply.
    x : 'a - The element to apply the function to.

Returns: 'a x, if the application of f x fails.

Repeats the application of a function f to an argument x until it fails.

f : 'a -> 'a

The function to apply.

x : 'a

The element to apply the function to.

Returns: 'a

x, if the application of f x fails.

Example

 repeat (List.removeAt 0) [1;2;3;4;5]
Multiple items
module List from Microsoft.FSharp.Collections

--------------------
type List<'T> = | op_Nil | op_ColonColon of Head: 'T * Tail: 'T list interface IReadOnlyList<'T> interface IReadOnlyCollection<'T> interface IEnumerable interface IEnumerable<'T> member GetReverseIndex: rank: int * offset: int -> int member GetSlice: startIndex: int option * endIndex: int option -> 'T list static member Cons: head: 'T * tail: 'T list -> 'T list member Head: 'T member IsEmpty: bool member Item: index: int -> 'T with get ...
val removeAt: index: int -> source: 'T list -> 'T list
Evaluates to [].