# 2015 005 Addition of Fn module

## Proposal 2015-005

### Addition of `Fn` module

Author: Andreas Rossberg
Last revised: September 21, 2015
Status: proposed
Discussion: issue #6

### Synopsis

```signature FN
structure Fn :> FN```

The `Fn` structure provides various combinators to aid computing with function values.

#### Interface

```val id       : 'a -> 'a
val const    : 'a -> 'b -> 'a
val apply    : ('a -> 'b) * 'a -> 'b
val o        : ('b -> 'c) * ('a -> 'b) -> ('a -> 'c)
val curry    : ('a * 'b -> 'c) -> ('a -> 'b -> 'c)
val uncurry  : ('a -> 'b -> 'c) -> ('a * 'b -> 'c)
val flip     : ('a * 'b -> 'c) -> ('b * 'a -> 'c)
val repeat   : int -> ('a -> 'a) -> ('a -> 'a)
val equal    : ''a -> ''a -> bool
val notEqual : ''a -> ''a -> bool```

#### Description

• `id x`
returns the value `x` (`id` is the polymorphic identity function).

• `const x y`
returns the value x.

• `apply (f, x)`
applies the function `f` to `x`. Thus, it is equivalent to `f a`.

• `f o g`
is the function composition of `f` and `g`. Thus, `(f o g) x` is equivalent to `f (g x)`. This function is the same as the global `o` operator and is also part of the `General` structure.

• `curry f x y`
is equivalent to `f (x, y)`; i.e., `curry f` transforms the binary function `f` into curried form.

• `uncurry f (x, y)`
is equivalent to `f x y`; i.e., `uncurry f` transforms the curried function `f` into a binary function. This function is the inverse of `curry`.

• `flip f (x, y)`
is equivalent to `f (y, x)`; i.e., `flip f` flips the argument order of the binary function `f`.

• `repeat n f`
returns the n-fold composition of `f`. If `n` is zero, then `repeat n f` returns the identity function. If `n` is negative, then it raises the exception `Domain`.

• `equal a b`
a curried version of the polymorphic equality function (`=`).

• `notEqual a b`
a curried version of the polymorphic inequality function (`<>`).

#### Discussion

Other combinators could be added. These are the ones that I have needed most in practice.

#### Impact

Adopting this proposal should not affect existing programs.

#### Rationale

The need for basic higher-order functions like `id` and `const` arises all the time in combination with other functionals. So far, the SML Basis has lacked any of these functions, although they are standard in the libraries of most other functional programming languages.

This module also provides a more natural home for the existing `o` operator, but it would also remain in the `General` structure for backward compatibility.

### History

• [2015-09-21] Added `equal` and `notEqual` functions as proposed by Michael Norrish in the discussion. [JHR].

• [2015-08-21] Reworked function descriptions to follow standard style [JHR].

• [2015-08-16] Proposed

##### Clone this wiki locally
You can’t perform that action at this time.
Press h to open a hovercard with more details.