[Haskell-cafe] Accepting and returning polyvariadic functions

Bartek Ćwikłowski paczesiowa at gmail.com
Wed Aug 11 14:43:55 EDT 2010

Hello Will,

2010/8/11 Will Jones <will at sacharissa.co.uk>:
> I'm trying to write a function (I'll call it `vtuple' for lack of a better
> name)
> that returns a function that itself returns multiple arguments in the form
> of a
> tuple. For example:
> vtuple f :: IO (Int -> (Int, ()))
> vtuple g :: IO (Int -> Int -> (Int, (Int, ())))

If we drop the IO (as pointed out by Ryan Ingram), vtuple seems weird
- the only sensible function of the type "Int -> Int -> (Int, (Int,
()))" is a function that collects its arguments and returns them in a
tuple, so it doesn't touch the input function g at all, it only cares
about g's arity.

Here's the solution:

> vtuple f = eat (arity f) `mcomp` hListToTuple

> class HListToTuple l r | l -> r where
>     hListToTuple :: l -> r
> instance HListToTuple HNil () where
>     hListToTuple _ = ()
> instance HListToTuple xs ys => HListToTuple (HCons x xs) (x,ys) where
>     hListToTuple (HCons x xs) = (x,hListToTuple xs)

Rest of the code (functions eat, arity and mcomp) is presented here:

Bartek Ćwikłowski

More information about the Haskell-Cafe mailing list