Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compose a binary function with a unary function?

I feel like I'm overlooking something totally obvious here but what is the correct way (if any) to use point-free notation for composing a binary function and a unary function? For example, the following code compiles:

sortedAppend :: (Ord a) -> [a] -> [a] -> [a]
sortedAppend xs ys = sort $ xs ++ ys

but the following code does not compile:

sortedAppend :: (Ord a) -> [a] -> [a] -> [a]
sortedAppend = sort . (++)

Are we able to compose (++) with sort (in the order shown above)? If so, how?

like image 711
iceman Avatar asked Nov 06 '14 18:11

iceman


People also ask

What is a unary function in c++?

A Unary Function is a kind of function object: an object that is called as if it were an ordinary C++ function. A Unary Function is called with a single argument.

What is unary real function?

A unary function is a function that takes one argument. A unary operator belongs to a subset of unary functions, in that its range coincides with its domain.


1 Answers

I don't think that any of these solutions (mine or the others) is that pretty, but I prefer....

let sortedAppend = (sort .) . (++)

The reason I prefer this is because it is easy for me to think of.... If you ignore the parenthesis, you basically need to add an extra (.) for each parameter

f . g --one parameter
f . . g --two params
f . . . g --three params

which makes sense, since g x returns a function with N-1 inputs....

....but those needed parens make it so ugly....

((f .) .) . g
like image 166
jamshidh Avatar answered Oct 07 '22 19:10

jamshidh