[Haskell-beginners] Function for working with functions in data?

Daniel Fischer daniel.is.fischer at googlemail.com
Fri May 6 21:27:22 CEST 2011


On Friday 06 May 2011 21:13:01, Brandon S Allbery KF8NH wrote:
> On 5/6/11 14:57 , Mike Meyer wrote:
> > I'm filling a functional itch that Haskell isn't scratching. I'm
> > looking for a function to take a list of functions, and apply them all
> > to a value. The signature is obvious :: [a -> b] -> a -> [b], and it's
> > nearly trivial to write (\fs a -> map ($ a) [fs]), but Haskell has
> > most useful list functions already provided.
> 
> It's there, but it's not a dedicated function; it's "sequence" applied
> to the environment monad (-> e).
> 
>     Prelude Control.Monad.Reader> :t sequence
>     sequence :: (Monad m) => [m a] -> m [a]
>     Prelude Control.Monad.Reader> :t sequence [(+1),(*2),(`subtract` 3)]
>     sequence [(+1),(*2),(`subtract` 3)] :: (Num a) => a -> [a]

The Monad instance for ((->) e) is defined in Control.Monad.Instances, so 
to use it, you must directly or indirectly (e.g. via Control.Monad.Reader 
or Control.Monad.State) import that.




More information about the Beginners mailing list