Skip to content

Instantly share code, notes, and snippets.

@clojj
Created January 4, 2016 21:33
Show Gist options
  • Select an option

  • Save clojj/7ec5107181fccd5f784e to your computer and use it in GitHub Desktop.

Select an option

Save clojj/7ec5107181fccd5f784e to your computer and use it in GitHub Desktop.
import Control.Parallel
import Data.List
pfold :: (Num a, Enum a) => (a -> a -> a) -> [a] -> a
pfold _ [x] = x
pfold mappend xs = (ys `par` zs) `pseq` (ys `mappend` zs) where
len = length xs
(ys', zs') = splitAt (len `div` 2) xs
ys = pfold mappend ys'
zs = pfold mappend zs'
main = print $ pfold (+) [ foldl' (*) 1 [1..x] | x <- [1..5000] :: [Integer]]
-- need a more complicated computation than (+) of numbers
-- so we produce a list of products of many numbers
-- ghc -O2 -threaded a.hs
-- a +RTS -N1 -s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment