SetOrd - module SetOrd...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: module SetOrd (Set(..),emptySet,isEmpty,inSet,subSet,insertSet, deleteSet,powerSet,takeSet,(!!!),list2set) where import List (sort) {-- Sets implemented as ordered lists without duplicates --} newtype Set a = Set [a] deriving (Eq,Ord) instance (Show a) => Show (Set a) where showsPrec _ (Set s) str = showSet s str showSet str = showString "{}" str showSet (x:xs) str = showChar '{' ( shows x ( showl xs str)) where showl str = showChar '}' str showl (x:xs) str = showChar ',' (shows x (showl xs str)) emptySet :: Set a emptySet = Set isEmpty :: Set a -> Bool isEmpty (Set ) = True isEmpty _ = False inSet :: (Ord a) => a -> Set a -> Bool inSet x (Set s) = elem x (takeWhile (<= x) s) subSet :: (Ord a) => Set a -> Set a -> Bool subSet (Set ) _ = True subSet (Set (x:xs)) set = (inSet x set) && subSet (Set xs) set insertSet :: (Ord a) => a -> Set a -> Set a insertSet x (Set s) = Set (insertList x s) insertList x = [x] insertList x ys@(y:ys') = case compare GT -> EQ -> _ -> x y of y : insertList x ys' ys x : ys deleteSet :: Ord a => a -> Set a -> Set a deleteSet x (Set s) = Set (deleteList x s) deleteList x = deleteList x ys@(y:ys') = case compare GT -> EQ -> _ -> x y of y : deleteList x ys' ys' ys list2set :: Ord a => [a] -> Set a list2set = Set list2set (x:xs) = insertSet x (list2set xs) -- list2set xs = Set (foldr insertList xs) powerSet :: Ord a => Set a -> Set (Set a) powerSet (Set xs) = Set (sort (map (\xs -> (list2set xs)) (powerList xs))) powerList powerList powerList :: [a] -> [[a]] = [] (x:xs) = (powerList xs) ++ (map (x:) (powerList xs)) takeSet :: Eq a => Int -> Set a -> Set a takeSet n (Set xs) = Set (take n xs) infixl 9 !!! (!!!) :: Eq a => Set a -> Int -> a (Set xs) !!! n = xs !! n ...
View Full Document

This note was uploaded on 11/10/2010 for the course CS 332F taught by Professor Russel during the Spring '10 term at California State University Los Angeles .

Page1 / 2

SetOrd - module SetOrd...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online