File: ListSet.hs

package info (click to toggle)
haskell-lazysmallcheck 0.6-15
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 208 kB
  • sloc: haskell: 1,230; sh: 15; makefile: 4
file content (34 lines) | stat: -rw-r--r-- 605 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module ListSet where

type Set a = [a]

empty :: Set a
empty = []

insert :: Ord a => a -> Set a -> Set a
insert a [] = [a]
insert a (x:xs)
  | a < x = a:x:xs
  | a > x = x:insert a xs
  | a == x = x:xs

set :: Ord a => [a] -> Set a
set = foldr insert empty

ordered [] = True
ordered [x] = True
ordered (x:y:zs) = x <= y && ordered (y:zs)

allDiff [] = True
allDiff (x:xs) = x `notElem` xs && allDiff xs

isSet s = ordered s && allDiff s

-- Properties

infixr 0 -->
False --> _ = True
True --> x = x

prop_insertSet :: (Char, Set Char) -> Bool
prop_insertSet (c, s) = ordered s --> ordered (insert c s)