The composition of functions is a way of combining two or more functions together. It is often compared to a shell. For example, in a Unix-style shell, you can write something like
cat foo.txt | sort -n | less
This starts cat , passes its output to sort and feeds the output from this to less .
Strictly, this is similar to the Haskell $ operator. You can write something like
sum $ sort $ filter (> 0) $ my_list
Note that unlike the shell example, this is read from right to left. So, we start with my_list as input, then filter on it, then we sort it, and then calculate its sum .
Function composition operator . does something like that. In the above example, a number is returned; in the example below the function is created:
sum . sort . filter (> 0)
Please note that we have not actually submitted a list. Instead, we just created a new function, and we can submit several different lists of this function. For example, you can name this function:
my_function = sum . sort . filter (> 0)
Or you can pass it as an argument to another function:
map (sum . sort . filter (> 0)) my_lists
You can use it anywhere you can use any other function. This is just a quick and easy way to say: "I want to combine these functions together."
MathematicalOrchid Sep 21 '12 at 7:36 2012-09-21 07:36
source share