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?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With