[Haskell-cafe] 'par' - why has it the type a -> b -> b ?
Bertram Felgenhauer
bertram.felgenhauer at googlemail.com
Mon Sep 29 18:50:50 EDT 2008
Henning Thielemann wrote:
>
> What is the reason for implementing parallelism with 'par :: a -> b -> b'?
> Analogy to 'seq'?
I'd think it's actually easier to implement than par2 below; evaluating
par x y "sparks" a thread evaluating x, and then returns y. The analogy
to 'seq' is there, of course.
> I thought parallelism would be introduced most naturally
> by a function which does two computations in parallel and puts together
> their results after completion. Say
>
>> par2 :: (a -> b -> c) -> (a -> b -> c)
>
> to be used like
>
>> par2 (+) expensiveComputationA expensiveComputationB
>
> I assume that par2 can be implemented this way:
>
>> par2 f x y =
>> f x (par x y)
For this to work, f has to evaluate its second argument before the
first one, or the par will be useless. Try this:
> par2 f x y = x `par` y `par` f x y
(In complete analogy to using `seq` for enforcing strictness.)
HTH,
Bertram
More information about the Haskell-Cafe
mailing list