Beispielcode für Sort und Typo
This commit is contained in:
parent
cd7ed5db2b
commit
d87db40eed
32
exampleCode/lecture8/sort.hs
Normal file
32
exampleCode/lecture8/sort.hs
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
module Main where
|
||||||
|
|
||||||
|
import Data.List
|
||||||
|
import Test.QuickCheck
|
||||||
|
|
||||||
|
-- Sorting twice changes nothing
|
||||||
|
prop_idempotency :: Ord a => [a] -> Bool
|
||||||
|
prop_idempotency xs = qsort xs == qsort (qsort xs)
|
||||||
|
|
||||||
|
-- Sorting doesn't change the length
|
||||||
|
prop_len :: Ord a => [a] -> Bool
|
||||||
|
prop_len xs = length xs == length (qsort xs)
|
||||||
|
|
||||||
|
-- Sorted result is a permutation of input
|
||||||
|
prop_perm :: Ord a => [a] -> Bool
|
||||||
|
prop_perm xs = (qsort xs) `elem` (permutations xs)
|
||||||
|
|
||||||
|
-- Sorting produces sorted list
|
||||||
|
prop_sort :: Ord a => [a] -> Bool
|
||||||
|
prop_sort = isSorted . qsort
|
||||||
|
where
|
||||||
|
isSorted :: Ord a => [a] -> Bool
|
||||||
|
isSorted [] = True
|
||||||
|
isSorted [x] = True
|
||||||
|
isSorted (x:y:zs) = (x <= y) && isSorted (y:zs)
|
||||||
|
|
||||||
|
qsort :: Ord a => [a] -> [a]
|
||||||
|
qsort [] = []
|
||||||
|
qsort (p:xs) = (qsort lesser) ++ p:(qsort greater)
|
||||||
|
where
|
||||||
|
lesser = filter (< p) xs
|
||||||
|
greater = filter (>= p) xs
|
BIN
lecture7.pdf
BIN
lecture7.pdf
Binary file not shown.
Loading…
Reference in New Issue
Block a user