1.7 KiB
1.7 KiB
Functions
Composition
$
($) :: (a -> b) -> a -> b
Infix form of function application. Applies a function from a → b to an
argument a.
Example:
> take 2 $ [1,2,3]
[1,2]
.
(.) :: (b -> c) -> (a -> b) -> a -> c
Function composition. Composes a function f (b → c) with a function
g (a → b) yielding f ∘ g.
Example:
> map (negate . abs) [-1,0,1]
[-1,0,-1]
&
(&) :: a -> (a -> b) -> b
Flipped form of ($) which applies an argument a to a function a → b.
Example:
> [1,2,3] & take 2
[1,2]
> replicate 10 3 & take 5 & tail
[3,3,3,3]
flip
flip :: (a -> b -> c) -> b -> a -> c
Flip takes a function of two arguments and returns a function taking the them in reverse order.
Example:
λ> flip take [1,2,3] 2
[1,2]
on
on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
Example:
> sortBy (compare `on` fst) [(1,2), (3,4), (0,1)]
[(0,1),(1,2),(3,4)]
const
const :: a -> b -> a
Example:
fix
fix :: (a -> a) -> a
Example:
identity
identity :: a -> a
The identity function maps any value to itself.
applyN
Apply a function to a value n times.
Example:
applyN :: Int -> (a -> a) -> a -> a
> applyN 25 (+2) 0
50
> applyN 3 (1:) []
[1,1,1]
Strictness
$!
($!) :: NFData a => (a -> b) -> a -> b
Example:
$!!
($!!) :: NFData a => (a -> b) -> a -> b
Example:
force
force :: NFData a => a -> a
Example: