[Haskell-cafe] List of functions
David Menendez
zednenem at psualum.com
Thu Sep 1 20:17:28 EDT 2005
Mark Goldman writes:
> On 8/31/05, Krasimir Angelov <kr.angelov at gmail.com> wrote:
> > 2005/8/31, Sebastian Sylvan <sebastian.sylvan at gmail.com>:
> > > On 8/31/05, Dinh Tien Tuan Anh <tuananhbirm at hotmail.com> wrote:
> > > >
> > > > >Something like (untested)...
> > > > >
> > > > >xs <- zipWith ($) forkIO (map (\f -> f x y) funs)
> > > > >tids <- sequence xs
> > > > >
> > > >
> > > >
> > > > what does "zipWith ($)" do ?
> > >
> > > $ is function application, so zipWith ($) will "zip" a list of
> > > functions with a list of arguments, by applying the functions to
the
> > > arguments pair-wise, producing a list of results.
> >
> > But forkIO is function not a list of functions. The above example is
> > incorrect. I think it should be:
> >
> > tids <- sequence [forkIO (f x y) | f <- funs]
>
> The following corrects the zipWith example:
> xs <- zipWith ($) (repeat forkIO) (map (\f -> f x y) funs)
> tids <- sequence xs
>
It's not always obvious when to use sequence or mapM, but I think this
one calls for the latter.
mapM (\f -> forkIO (f x y)) funs :: IO [ThreadId]
If you don't care about the thread IDs, then this is more efficient:
mapM_ (\f -> forkIO (f x y)) funs :: IO ()
--
David Menendez <zednenem at psualum.com> <http://www.eyrie.org/~zednenem/>
More information about the Haskell-Cafe
mailing list