diff options
| author | Eugen Wissner <belka@caraus.de> | 2025-12-11 10:28:11 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2025-12-11 10:28:11 +0100 |
| commit | 98329e0a3dd4f78b5d815ac3896272ec70904901 (patch) | |
| tree | 80f9c56cfe2ac20232358f236d32e84bd683be1b /Haskell-book/14/qc/src/UsingQuickCheck.hs | |
| parent | 3624c712d72d246f21d4e710cec7c11e052e0326 (diff) | |
| download | book-exercises-98329e0a3dd4f78b5d815ac3896272ec70904901.tar.gz | |
Add remaining haskell book exercises
Diffstat (limited to 'Haskell-book/14/qc/src/UsingQuickCheck.hs')
| -rw-r--r-- | Haskell-book/14/qc/src/UsingQuickCheck.hs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/Haskell-book/14/qc/src/UsingQuickCheck.hs b/Haskell-book/14/qc/src/UsingQuickCheck.hs new file mode 100644 index 0000000..f0fa27f --- /dev/null +++ b/Haskell-book/14/qc/src/UsingQuickCheck.hs @@ -0,0 +1,58 @@ +module UsingQuickCheck where + +import Test.QuickCheck + +-- +-- 1 +-- +half :: (Eq a, Fractional a) => a -> a +half x = x / 2 + +halfIdentity :: (Eq a, Fractional a) => a -> a +halfIdentity = (*2) . half + +-- +-- 2 +-- +-- for any list you apply sort to +-- this property should hold +listOrdered :: (Ord a) => [a] -> Bool +listOrdered xs = + snd $ foldr go (Nothing, True) xs + where go _ status@(_, False) = status + go y (Nothing, t) = (Just y, t) + go y (Just x, _) = (Just y, x >= y) + +-- +-- 3 +-- +plusAssociative :: (Ord a, Integral a) => a -> a -> a -> Bool +plusAssociative x y z = x + (y + z) == (x + y) + z + +plusCommutative :: (Ord a, Integral a) => a -> a -> Bool +plusCommutative x y = x + y == y + x + +-- +-- 4 +-- +mulAssociative :: (Ord a, Integral a) => a -> a -> a -> Bool +mulAssociative x y z = x * (y * z) == (x * y) * z + +mulCommutative :: (Ord a, Integral a) => a -> a -> Bool +mulCommutative x y = x * y == y * x + +data Fool = Fulse + | Frue + deriving (Eq, Show) + +data Fool' = Fulse' -- 2/3 + | Frue' -- 1/3 + deriving (Eq, Show) + +instance Arbitrary Fool where + arbitrary = oneof [ return Fulse + , return Frue ] + +instance Arbitrary Fool' where + arbitrary = frequency [ (3, return Fulse') + , (1, return Frue')]
\ No newline at end of file |
