-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting.hs
31 lines (26 loc) · 859 Bytes
/
sorting.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
import Test.Extrapolate
sort :: Ord a => [a] -> [a]
sort [] = []
sort (x:xs) = sort (filter (< x) xs)
++ [x]
++ sort (filter (> x) xs)
prop_sortOrdered :: Ord a => [a] -> Bool
prop_sortOrdered xs = ordered (sort xs)
where
ordered (x:y:xs) = x <= y && ordered (y:xs)
ordered _ = True
prop_sortCount :: Ord a => a -> [a] -> Bool
prop_sortCount x xs = count x (sort xs) == count x xs
count :: Eq a => a -> [a] -> Int
count x = length . filter (== x)
main :: IO ()
main = do
chk $ prop_sortOrdered -:> [int]
chk $ prop_sortCount -:> int
check $ prop_sortCount -:> int
chk `for` 99 $ prop_sortOrdered -:> [bool]
chk `for` 99 $ prop_sortCount -:> bool
where
chk :: Testable a => a -> IO ()
chk = check `withBackground` [ value "count" $ count -:> int ]
`withConditionSize` 6