[Haskell-cafe] par / pseq example

J. Waldmann waldmann at imn.htwk-leipzig.de
Wed Dec 15 11:30:32 CET 2010


I was trying to find an easy example for par/pseq,
and I finally used plain mergesort ( on Prelude.[] )

and I observed that it is best to give +RTS -A1G (or more)
such that there are only very few garbage collections.
This roughly cuts execution time in half 
(from +RTS -N1 to -N4, on an i7 CPU)
which is nice for a demonstration.

The following is the very straightforward program that I use.
Is it OK? I'm not asking for a faster sort algorithm,
but whether par/pseq is used correctly/idiomatically in this program
(so that students don't get a wrong impression).

msort [] = [] ; msort [x] = [x]
msort xs = 
    let ( here, there ) = splitAt ( div ( length xs ) 2 ) xs
        mshere = msort here
        msthere = msort there
    in        last mshere 
        `par` last msthere
        `pseq` merge mshere msthere 

merge [] ys = ys ; merge xs [] = xs
merge (x:xs) (y:ys) = 
    if x < y then x : merge xs (y:ys)
             else y : merge (x:xs) ys

Thanks - J.W.




More information about the Haskell-Cafe mailing list