-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStdList.hs
72 lines (64 loc) · 2 KB
/
StdList.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
-----------------------------------------------------------------------------
-- |
-- Module : StdList
-- Author : Prabhat Totoo 2011
--
-- Functions and benchmark applications based on standard list.
--
-----------------------------------------------------------------------------
module StdList (
-- * Seq. functions
-- | Sequential operations on standard list.
histo, histo', qsort, update,
-- * Par. functions
-- | Parallel operations on standard list.
parsum, parmin, parelem
) where
import Data.List
import Control.DeepSeq
import Control.Parallel
import Control.Parallel.Strategies
-- Seq. functions
histo :: Int -> [Int] -> [Int]
histo n randlist = map (\i ->length $ filter (== i) randlist) [0..n-1]
histo' :: Int -> [Int] -> [Int]
histo' n randlist = foldl inc (replicate n 0) randlist
where
inc xs i = a ++ [y] ++ tail b
where
y = head b + 1
(a,b) = splitAt i xs
qsort :: Ord a => [a] -> [a]
qsort [] = []
qsort [x] = [x]
qsort (x:xs) = losort ++ (x:hisort)
where
losort = qsort [y|y<-xs,y<x]
hisort = qsort [y|y<-xs,y>=x]
update::Int -> a -> [a] -> [a]
update i x list = take i list ++ [x] ++ drop i list
-- Par. functions
parsum :: Num a => [a] -> a
parsum [] = 0
parsum [x] = x
parsum xs = left `par` (right `pseq` (left + right))
where
left = parsum $ take half xs
right = parsum $ drop half xs
half = length xs `div` 2
parmin :: (Ord a) => [a] -> a
parmin xs | cutoff = minimum xs
| otherwise = left `par` (right `pseq` min left right)
where
left = parmin $ take half xs
right = parmin $ drop half xs
half = length xs `div` 2
cutoff = length xs < 100
parelem :: (Eq a) => a -> [a] -> Bool
parelem x xs | cutoff = elem x xs
| otherwise = left `par` (right `pseq` left || right)
where
left = parelem x $ take half xs
right = parelem x $ drop half xs
half = length xs `div` 2
cutoff = length xs < 100