Haskell function composition

I am reading this Haskell tutorial. They determine the composition of functions as follows:

(.) :: (b->c) -> (a->b) -> (a->c) f . g = \ x -> f (gx) 

No examples were provided, which I think will enlighten me as to what is defined here.

Can someone provide a simple example (with explanation) of how function composition is used?

+19
function composition functional-programming haskell
Sep 25 '09 at 7:27
source share
6 answers

The composition of functions is a way to β€œassemble” two functions together into one function. Here is an example:

Say you have the following functions:

 even :: Int -> Bool not :: Bool -> Bool 

and you want to define your own function myOdd :: Int -> Bool using the above.

The obvious way to do this is as follows:

 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 is created by "gluing" the two functions together.

A scenario in which this is especially useful is to remove the need for an explicit lambda. For example:

 map (\x -> not (even x)) [1..9] 

can be rewritten to:

 map (not . even) [1..9] 

A little shorter, less room for mistakes.

+38
Sep 25 '09 at 7:39
source share

Wonderful side. The composition of the function is the equivalent of syllogism in logic:

All people are mortal. Socrates is a man. Therefore Socrates is mortal.

Syllogism is two significant meanings in one:

 (Man => Mortal), (Socrates => Man), therefore (Socrates => Mortal) 

Therefore...

 (B => C) => (A => B) => (A => C) 

... which is a type of function . .

+23
Oct 11 '09 at 6:13
source share

the composition of f and g is a function that first applies g to its argument, and then f to the value returned by g . Then it returns the return value of f .

This identity can be enlightening:

 f (gx) = (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)); } 
+13
Sep 25 '09 at 7:34
source share

This example is contrived, but suppose that

 sqr x = x * x inc x = x + 1 

and we want to write a function that computes x ^ 2 + 1. We can write

 xSquaredPlusOne = inc . sqr 

(which means

 xSquaredPlusOne x = (inc . sqr) x 

which means

 xSquaredPlusOne x = inc(sqr x) 

since f = inc and g = sqr).

+7
Sep 25 '09 at 7:37
source share

From the HaskellWiki page about the composition of the function:

 desort = (reverse . sort) 

Now desort is a function that sorts the list in reverse order. Basically, desort passes its arguments to sort , and then returns the return value from sort to reverse , returns this. So he sorts it and then changes the sorted list.

+4
Sep 25 '09 at 7:35
source share

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."

+3
Sep 21 '12 at 7:36
source share



All Articles