In high-performance computing, sums, products, etc are often calculated using a \"parallel reduction\" that takes n elements and completes in O(log n) time
Not sure what your parFold
function is supposed to do. If that is intended to be a parallel version of foldr or foldl, I think its definition is wrong.
parFold :: (a -> a -> a) -> [a] -> a
// fold right in haskell (takes 3 arguments)
foldr :: (a -> b -> b) -> b -> [a] -> b
Fold applies the same function to each element of the list and accumulates the result of each application. Coming up with a parallel version of it, i guess, would require that the function application to the elements are done in parallel - a bit like what parList
does.
par_foldr :: (NFData a, NFData b) => (a -> b -> b) -> b -> [a] -> b
par_foldr f z [] = z
par_foldr f z (x:xs) = res `using` \ _ -> rseq x' `par` rdeepseq res
where x' = par_foldr f z xs
res = x `f` x'