Skip to content

Instantly share code, notes, and snippets.

@bitwombat
Created July 25, 2019 02:15
Show Gist options
  • Save bitwombat/795d0714e4aa81fa15668c68d19b49a1 to your computer and use it in GitHub Desktop.
Save bitwombat/795d0714e4aa81fa15668c68d19b49a1 to your computer and use it in GitHub Desktop.
Page 562 exercises
module Page562Tests where
import Page562
import Test.QuickCheck
import Data.List (sort)
--Ex 1
halfIdentity :: (Fractional a, Num a) => a -> a
halfIdentity = (*2) . half
prop_halfIdentity :: Float -> Bool
prop_halfIdentity x = x == halfIdentity x
--Ex 2
listGen :: Arbitrary => Gen [a]
listGen = do
a <- arbitrary
return (a)
listIsOrdered :: [a] -> Bool
listIsOrdered xs =
snd $ foldr go (Nothing, True) xs
where go _ status@(_, False) = status
go y (Nothing, t) = (Just y, t)
go y (Just x, t) = (Just y, x >= y)
prop_listOrdered :: Property
prop_listOrdered =
forAll listGen (\l -> (listIsOrdered $ sort l))
runQC :: IO ()
runQC = do
quickCheck prop_halfIdentity
quickCheck prop_listOrdered
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment