Movatterモバイル変換


[0]ホーム

URL:


base-4.12.0.0: Basic libraries

CopyrightNils Anders Danielsson 2006
Alexander Berntsen 2014
LicenseBSD-style (see the LICENSE file in the distribution)
Maintainerlibraries@haskell.org
Stabilityexperimental
Portabilityportable
Safe HaskellTrustworthy
LanguageHaskell2010

Data.Function

Contents

Description

Simple combinators working solely on and with functions.

Synopsis
  • id :: a -> a
  • const :: a -> b -> a
  • (.) :: (b -> c) -> (a -> b) -> a -> c
  • flip :: (a -> b -> c) -> b -> a -> c
  • ($) ::forall r a (b ::TYPE r). (a -> b) -> a -> b
  • (&) :: a -> (a -> b) -> b
  • fix :: (a -> a) -> a
  • on :: (b -> b -> c) -> (a -> b) -> a -> a -> c

Prelude re-exports

id :: a -> aSource#

Identity function.

id x = x

const :: a -> b -> aSource#

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]

(.) :: (b -> c) -> (a -> b) -> a -> cinfixr 9Source#

Function composition.

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

flip f takes its (first) two arguments in the reverse order off.

>>>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$ x). However,$ 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 asmap ($ 0) xs, orzipWith ($) fs xs.

Note that($) is levity-polymorphic in its result type, so that foo $ True where foo :: Bool -> Int# is well-typed

Other combinators

(&) :: a -> (a -> b) -> binfixl 1Source#

& is a reverse application operator. This provides notational convenience. Its precedence is one higher than that of the forward application operator$, which allows& to be nested in$.

>>>5 & (+1) & show"6"

Since: 4.8.0.0

fix :: (a -> a) -> aSource#

fix f is the least fixed point of the functionf, 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#

on b u x y runs the binary functionbon the results of applying unary functionu to two argumentsx andy. From the opposite perspective, it transforms two inputs and combines the outputs.

((+) `on` f) x y = f x + f y

Typical usage:sortBy (compare `on`fst).

Algebraic properties:

  • (*) `on`id = (*) -- (if (*) ∉ {⊥,const ⊥})
  • ((*) `on` f) `on` g = (*) `on` (f . g)
  • flip on f .flip on g =flip on (g . f)

Produced byHaddock version 2.20.0


[8]ページ先頭

©2009-2025 Movatter.jp