Personal tools

Composing functions with multiple values

From HaskellWiki

Revision as of 09:50, 4 December 2006 by Lemming (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

1 Question

I see a certain imbalance between multiple function arguments and multiple function values. On the one hand functions with multiple arguments are often in curried form, like

(++) :: [a] -> [a] -> [a]

which allows for partial application. On the other hand, functions cannot emit values in a compatible way, and thus must return pairs instead. Example:

partition :: (a -> Bool) -> [a] -> ([a], [a])
How can I convert the result of
partition
such that it meets the needs of
(++)
?

2 Answer

You can't convert a pair to something that fits a function that accepts two arguments in curried form. But you can convert the curried function

to one that accepts pairs using the standard function
uncurry
!

It is

uncurry (++) :: ([a], [a]) -> [a]

and consequently

uncurry (++) . partition p :: [a] -> [a]

is what you are after.

In fact, there is no imbalance. Functions always accept exactly one input and emit exactly one output. You can always choose tuples as both input and output. Using tuples as input is close to the way other languages handle multiple inputs. If you choose the curried form you do that because of partial application. In this case you should also think intensively about how to order the parameters in order to make partial application useful.