Function for working with functions in data?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Function for working with functions in data?

Mike Meyer
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.

I couldn't turn it up in hoogle or the prelude. Am I just missing it
somewhere, or is it really not available in a library?

   Thanks,
   <mike
--
Mike Meyer <mwm at mired.org> http://www.mired.org/
Independent Software developer/SCM consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org


Reply | Threaded
Open this post in threaded view
|

Function for working with functions in data?

Brandon Allbery
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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]

- --
brandon s. allbery     [linux,solaris,freebsd,perl]    allbery.b at gmail.com
system administrator  [openafs,heimdal,too many hats]                kf8nh
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk3ESD0ACgkQIn7hlCsL25XFeQCgnD2GTT4sjYKJ5Nw9QGG+qGJb
8NkAoKq07s8sUxjqw3CthG/0HNiKwnlU
=ifKQ
-----END PGP SIGNATURE-----


Reply | Threaded
Open this post in threaded view
|

Function for working with functions in data?

Daniel Fischer
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.