Haskell function from (a -> [b]) -> [a -> b]

function, haskell, list

Solution

You can generalize `seperateFuncs` to `Applicative` (or `Monad`) pretty cleanly:

seperateFuncs :: (Applicative f) => f (a -> b) -> (a -> f b)
seperateFuncs f x = f <*> pure x

Written in point-free style, you have `seperateFuncs = ((. pure) . (<*>))`, so you basically want `unap . (. extract)`, giving the following definition if you write it in pointful style:

joinFuncs :: (Unapplicative f) => (a -> f b) -> f (a -> b)
joinFuncs f = unap f (\ g -> f (extract g))

Here I define `Unapplictaive` as:

class Functor f => Unapplicactive f where
    extract  :: f a -> a
    unap     :: (f a -> f b) -> f (a -> b)

To get the definitions given by leftaroundabout, you could give the following instances:

instance Unapplicative [] where
    extract = head
    unap f = [\a -> f [a] !! i | i <- [0..]]

instance Unapplicative ((->) c) where
    extract f = f undefined
    unap f = \x y -> f (const y) x

I think it's hard to come up with a "useful" function `f :: (f a -> f b) -> f (a -> b)` for any `f` that isn't like `(->)`.

Problem

I have a function `seperateFuncs` such that ``` seperateFuncs :: [a -> b] -> (a -> [b]) seperateFuncs xs = \x -> map ($ x) xs ``` I was wondering whether the converse existed, i.e. is there a function ``` joinFuncs :: (a -> [b]) -> [a -> b] ``` I think not (mainly because lists are not fixed length), but perhaps I'll be proved wrong. The question then is there some datatype `f` which has a function :: (a -> f b) -> f (a -> b)?

Original source

Related problems