parMap :: (a -> b) -> [a] -> [b]
parMap f [] = []
parMap f (x:xs) = fx `par` fxs `seq` (fx:fxs)
where
fx = f x
fxs = parMap f xs
The definition above works as follows: fx is sparked, before recursing down the list (fxs), only returning the first constructor of the result list after every element has been sparked. The process diagram for parMap is given in the figure below. If the function argument supplied to parMap constructs a data structure, it must be composed with a forcing function in order to ensure that the data structure is constructed in parallel.
|
|
|