module Lecture02A where
import Data.List(sort)
import Test.QuickCheck
import VerboseCheck
-- local module: download from homepage
import Prelude hiding ((++),reverse,take,drop)
-- hide some standard functions so we
-- can redefine them
{-
Plan:
Lists; recursive definitions, polymorphism,
classes, quickCheck
What are lists?
-}
-- example list functions from Prelude
-- append
(++) :: [a] -> [a] -> [a]
[] ++ ys = ys
(x:xs) ++ ys = x:(xs++ys)
prop_appendNil :: [Bool] -> Bool
prop_appendNil xs = xs ++ [] == xs
prop_lenAppend xs ys = length (xs ++ ys)
== length xs + length ys
-- reverse
-- (inefficient version: see next lecture for discussion)
reverse :: [a] -> [a]
reverse [] = []
reverse (x:xs) = reverse xs ++ [x]
prop_rev2 :: [Bool] -> Bool
prop_rev2 xs = reverse (reverse xs) == xs
prop_pal :: [Bool] -> Bool
prop_pal xs = p == reverse p
where p = xs ++ reverse xs
-- take, drop
take,drop :: Int -> [a] -> [a]
take n xs | n <= 0 = []
take _ [] = []
take n (x:xs) = x : take (n-1) xs
prop_take :: Int -> [Integer] -> Property
prop_take n xs = n >= 0 ==>
classify (n >= 2 * length xs)
"big n"
$
length (take n xs) <= n
drop = undefined
-- simple property of take? (==>)
-- connecting take and drop?
isort,qsort :: (Ord a) => [a] -> [a]
qsort [] = []
qsort (x:xs) = qsort small ++ [x] ++ qsort big
where small = [y | y <- xs, y < x]
big = [z | z <- xs, z >= x]
prop_qsort xs = qsort xs == sort xs
where types = xs :: [Integer]
---------------------------------------
-- isort
isort [] = []
isort (x:xs) = insert x (isort xs)
insert :: Ord a => a -> [a] -> [a]
insert x [] = [x]
insert x (y:ys) | x < y = x:y:ys
| x >= y = y:insert x ys
-- Not good because most random sorted lists will be
-- very small.
prop_insert x xs = xs == sort xs ==>
insert x xs == sort (x:xs)
where types = x :: Integer
-- property of insert; using collect (classify?)
---------------------------------------
-- Are lists special? [slides]
-- c.f. data Hand = Empty | Add Card Hand
--