-
Notifications
You must be signed in to change notification settings - Fork 0
/
exerc7.hs
46 lines (33 loc) · 1.22 KB
/
exerc7.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
{-# LANGUAGE DeriveDataTypeable #-}
import Data.List
import Data.Data
import Data.Function
type Priority = Int
data Entry a = Entry Priority a
deriving (Eq, Show, Data, Typeable)
data PQueue a = PQueue [Entry a]
deriving (Eq, Show, Data, Typeable)
(@@) :: a -> Priority -> Entry a
e @@ p = Entry p e
value :: Entry a -> a
value (Entry p e) = e
priority :: Entry a -> Priority
priority (Entry p e) = p
fromList :: (Eq a) => [(Priority, a)] -> PQueue a
fromList l = PQueue $ map (\(p,e) -> e @@ p) $ concat $ groupBy (\(p1,e1) (p2,e2) -> p1 == p2) $ sortBy (flip compare `on` (\(p,e) -> p)) l
priq :: PQueue String
priq = fromList [(10, "ten"), (9, "nine"), (8, "eight"), (2, "two")]
push :: Entry a -> PQueue a -> PQueue a
push e (PQueue (p:ps))
| priority e > priority p = PQueue (e:p:ps)
| length ps == 0 = PQueue (p:e:ps)
| priority e <= priority p = PQueue (p:((\(PQueue l) -> l) $ push e (PQueue ps)))
top :: PQueue a -> Maybe (Entry a)
top (PQueue []) = Nothing
top (PQueue (p:ps)) = Just p
remove :: PQueue a -> PQueue a
remove (PQueue []) = PQueue []
remove (PQueue (p:ps)) = PQueue ps
pop :: PQueue a -> (Maybe (Entry a), PQueue a)
pop (PQueue []) = (Nothing, PQueue [])
pop (PQueue (p:ps)) = (Just p, PQueue ps)