Function composition

From HaskellWiki

Function composition is the act of pipelining the result of one function, to the input of another, creating an entirely new function.

Discussion and example[edit]

This can be done with any two functions, where the argument type of the first is the return type of the second. The newly created function takes what the second function would as a parameter and feeds it through the second function, then the result of the second function through the first function, and returns the result of the first function.

Mathematically, this is most often represented by the Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \circ} operator, where Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle f\circ g} (often read as f of g) is the composition of Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle f} with Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle g} .

In haskell, the type of the . operator is Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle (\beta \to \gamma) \to (\alpha \to \beta) \to \alpha \to \gamma}

Further math related items at Wolfram's composition page

Example[edit]

-- the '.' operator is used to compose functions

-- result of sort is pipelined to reverse
desort = (reverse . sort)

-- the result is a descending sort
countdown = desort [2,8,7,10,1,9,5,3,4,6]

A simpler way of thinking about it, is that reverse . sort is equivalent to \x -> reverse (sort x), the benefit is that the meaning of composition is obvious, and the representation is compact.