-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.hs
93 lines (76 loc) · 2.59 KB
/
02.hs
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
-------------------------------------------------------------------------------
-- 2.1 - Linked lists (stacks)
class Stack s where
empty :: s a
isEmpty :: s a -> Bool
cons :: a -> s a -> s a
hd :: s a -> a
tl :: s a -> s a
cat :: s a -> s a -> s a
tails :: s a -> s (s a)
update :: s a -> Int -> a -> s a
-- The native list
instance Stack [] where
empty = []
isEmpty = null
cons = (:)
hd = head
tl = tail
cat = (++)
tails [] = [[]]
tails w@(_:xs) = w : tails xs
update [] _ _ = error "Bad subscript."
update (x:xs) 0 y = y:xs
update (x:xs) i y = x : update xs (i-1) y
-- A custom list/stack
data List a = Nil | Cons a (List a) deriving (Eq, Show, Read)
instance Stack List where
empty = Nil
isEmpty Nil = True
isEmpty _ = False
cons = Cons
hd Nil = error "Empty list has no head."
hd (Cons x _) = x
tl Nil = error "Empty list has no tail."
tl (Cons _ xs) = xs
cat Nil ys = ys
cat (Cons x xs) ys = Cons x (cat xs ys)
tails Nil = Nil
tails (Cons x xs) = Cons x xs `Cons` tails xs
update Nil _ _ = error "Bad subscript."
update (Cons x xs) 0 y = Cons y xs
update (Cons x xs) i y = x `Cons` update xs (i-1) y
-------------------------------------------------------------------------------
-- 2.2 - Binary search trees
data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Eq, Show, Read)
class Set s a where
emptySet :: s a
insert :: s a -> a -> s a
isMember :: s a -> a -> Bool
instance (Ord a) => Set Tree a where
emptySet = Empty
insert Empty x = Node x Empty Empty
insert t@(Node y less more) x
| x < y = Node y (insert less x) more
| x > y = Node y less (insert more x)
| otherwise = Node y less more
isMember Empty x = False
isMember (Node y less more) x
| x < y = isMember less x
| x > y = isMember more x
| otherwise = True
complete :: (Ord a) => a -> Int -> Tree a
complete _ 0 = Empty
complete x d = Node x child child
where child = complete x (d-1)
-- data KV k v = KV k v
-- instance (Eq k) => Eq (KV k v) where
-- (KV k1 _) == (KV k2 _) = k1 == k2
-- instance (Ord k, Eq k) => Ord (KV k v) where
-- (KV k1 _) `compare` (KV k2 _) = k1 `compare` k2
-- class FiniteMap m x where
-- emptyMap :: m (KV k v)
-- bind :: k -> v -> m (KV k v) -> m (KV k v)
-- lookup :: k -> m (KV k v) -> v
-- instance (Ord k, Eq k) => FiniteMap