Modulecomposition-prelude-3.0.0.2Haskell98
Control.Composition
- 42 values
- Packagecomposition-prelude-3.0.0.2
- Exports42
- LanguageHaskell98
- LicenceBSD-3-Clause
- SourceComposition.hs
Postcomposition
6 declarationsPrecomposition
6 declarationsBackwards function composition. This is a specialization of <&>, but it has a different fixity.
Monadic postcomposition
4 declarationsThe bleeding fish operator
Compare >=>.
Monadic precomposition
4 declarationsBetween combinators
4 declarationsCan be used to rewrite
\g -> f . g . hto
between f hFancy function application
1 declarationBackwards function application. This is an infix synonym for flip
Monadic helpers
2 declarationsInfix version of join
As an example, one could use this to rewrite
between (char '"') (char '"')to
between .$ (char '"')Or
fromEither :: Either a a -> a
fromEither = either id idto
fromEither :: Either a a -> a
fromEither = either .$ idMonadic actions
2 declarationsComposition with lists of functions
2 declarationsTuple helpers
3 declarationsInfix synonym for both
J inspired
1 declarationPronounced 'appose'. Synonym for on
Reëxports from base
7 declarationsA variant of <*> with the types of the arguments reversed. It differs from
flip in that the effects are resolved in the order the arguments are
presented.(<*>)
Examples
(<**>) (print 1) (id <$ print 2)12
flip (<*>) (print 1) (id <$ print 2)21
ZipList [4, 5, 6] <**> ZipList [(+1), (*2), (/3)]ZipList {getZipList = [5.0,10.0,2.0]}
& 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 $.
This is a version of flip id, where id is specialized from a -> a to (a -> b) -> (a -> b)
which by the associativity of (->) is (a -> b) -> a -> b.
flipping this yields a -> (a -> b) -> b which is the type signature of &
Examples
5 & (+1) & show"6"
sqrt $ [1 / n^2 | n <- [1..1000]] & sum & (*6)3.1406380562059946
fix f is the least fixed point of the function f,
i.e. the least defined x such that f x = x.
When f is strict, this means that because, by the definition of strictness,
f ⊥ = ⊥ and such the least defined fixed point of any strict function is ⊥.
Examples
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’s let introduces recursive bindings. We can
rewrite this definition using fix,
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.
fix (\rec n -> if n <= 1 then 1 else n * rec (n-1)) 5120
Using fix, we can implement versions of repeat as fix .
and cycle as (:)fix . (++)
take 10 $ fix (0:)[0,0,0,0,0,0,0,0,0,0]
map (fix (\rec n -> if n < 2 then n else rec (n - 1) + rec (n - 2))) [1..10][1,1,2,3,5,8,13,21,34,55]
Implementation Details
The current implementation of fix uses structural sharing
fix f = let x = f x in xA more straightforward but non-sharing version would look like
fix f = f (fix f)on b u x y runs the binary function b on the results of applying
unary function u to two arguments x and y. From the opposite
perspective, it transforms two inputs and combines the outputs.
(op `on` f) x y = f x `op` f yExamples
sortBy (compare `on` length) [[0, 1, 2], [0, 1], [], [0]][[],[0],[0,1],[0,1,2]]
((+) `on` length) [1, 2, 3] [-1]4
((,) `on` (*2)) 2 3(4,6)