Haskell function composition
I am reading this tutorial on Haskell. They define function composition as the following:
(.) :: (b->c) -> (a->b) -> (a->c)
f . g = x -> f (g x)
No examples were provided, which I believe would enlighten me as to what is being defined here.
Can someone provide a simple example (with explanation) of how function composition is used?
Function composition is a way to "compose" two functions together into a single function. Here's an example:
Say you have these functions:
even :: Int -> Bool
not :: Bool -> Bool
and you want to define your own myOdd :: Int -> Bool
function using the two above.
The obvious way to do this is the following:
myOdd :: Int -> Bool
myOdd x = not (even x)
But this can be done more succinctly using function composition:
myOdd :: Int -> Bool
myOdd = not . even
The myOdd
functions behave exactly the same, but the second one is created by "glue-ing" two functions together.
A scenario where this is especially useful is to remove the need for an explicit lambda. Eg:
map (x -> not (even x)) [1..9]
can be rewritten to:
map (not . even) [1..9]
A bit shorter, less room for errors.
Fun side note. Function composition is the equivalent of a syllogism in logic:
All men are mortal. Socrates is a man. Therefore, Socrates is mortal.
A syllogism composes two material implications into one:
(Man => Mortal), (Socrates => Man), therefore (Socrates => Mortal)
Therefore...
(b -> c) -> (a -> b) -> (a -> c)
... which is the type of the .
function.
The composition of f
and g
is a function that first applies g
to its argument, then f
to the value returned by g
. It then returns the return value of f
.
This identity may be enlightening:
f (g x) = (f . g) x
If you have a Java/C background, consider this example:
int f(int x);
int g(int x);
int theComposition(int x) { return f(g(x)); }
链接地址: http://www.djcxy.com/p/80720.html
下一篇: Haskell函数组成