Copyright | Nils Anders Danielsson 2006 Alexander Berntsen 2014 |
---|---|

License | BSD-style (see the LICENSE file in the distribution) |

Maintainer | libraries@haskell.org |

Stability | experimental |

Portability | portable |

Safe Haskell | Trustworthy |

Language | Haskell2010 |

- Prelude re-exports
- Other combinators

Simple combinators working solely on and with functions.

# Prelude re-exports

`const x`

is a unary function which evaluates to `x`

for all inputs.

`>>>`

42`const 42 "hello"`

`>>>`

[42,42,42,42]`map (const 42) [0..3]`

flip :: (a -> b -> c) -> b -> a -> c Source #

takes its (first) two arguments in the reverse order of `flip`

f`f`

.

`>>>`

"worldhello"`flip (++) "hello" "world"`

($) :: forall r a (b :: TYPE r). (a -> b) -> a -> b infixr 0 Source #

Application operator. This operator is redundant, since ordinary
application `(f x)`

means the same as `(f `

. However, `$`

x)`$`

has
low, right-associative binding precedence, so it sometimes allows
parentheses to be omitted; for example:

f $ g $ h x = f (g (h x))

It is also useful in higher-order situations, such as

,
or `map`

(`$`

0) xs

.`zipWith`

(`$`

) fs xs

Note that `(`

is levity-polymorphic in its result type, so that
`$`

)`foo `

where `$`

True`foo :: Bool -> Int#`

is well-typed.

# Other combinators

is the least fixed point of the function `fix`

f`f`

,
i.e. the least defined `x`

such that `f x = x`

.

For example, we can write the factorial function using direct recursion as

`>>>`

120`let fac n = if n <= 1 then 1 else n * fac (n-1) in fac 5`

This uses the fact that Haskell’s `let`

introduces recursive bindings. We can
rewrite this definition using `fix`

,

`>>>`

120`fix (\rec n -> if n <= 1 then 1 else n * rec (n-1)) 5`

Instead of making a recursive call, we introduce a dummy parameter `rec`

;
when used within `fix`

, this parameter then refers to `fix`

’s argument, hence
the recursion is reintroduced.

on :: (b -> b -> c) -> (a -> b) -> a -> a -> c infixl 0 Source #