| 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 |
Data.Function
Contents
Description
Simple combinators working solely on and with functions.
const x is a unary function which evaluates tox for all inputs.
>>>const 42 "hello"42
>>>map (const 42) [0..3][42,42,42,42]
flip :: (a -> b -> c) -> b -> a -> cSource#
takes its (first) two arguments in the reverse order offlip ff.
>>>flip (++) "hello" "world""worldhello"
($) ::forall r a (b ::TYPE r). (a -> b) -> a -> binfixr 0Source#
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, ormap ($ 0) xs.zipWith ($) fs xs
Note that($) is levity-polymorphic in its result type, so that foo $ True where foo :: Bool -> Int# is well-typed
is the least fixed point of the functionfix ff, i.e. the least definedx such thatf x = x.
For example, we can write the factorial function using direct recursion as
>>>let fac n = if n <= 1 then 1 else n * fac (n-1) in fac 5120
This uses the fact that Haskell’slet introduces recursive bindings. We can rewrite this definition usingfix,
>>>fix (\rec n -> if n <= 1 then 1 else n * rec (n-1)) 5120
Instead of making a recursive call, we introduce a dummy parameterrec; when used withinfix, this parameter then refers tofix' argument, hence the recursion is reintroduced.
on :: (b -> b -> c) -> (a -> b) -> a -> a -> cinfixl 0Source#
Produced byHaddock version 2.20.0