-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQueueState.hs
63 lines (41 loc) · 1.58 KB
/
QueueState.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
-- Haskell: The Craft of Functional Programming
-- Simon Thompson
-- (c) Addison-Wesley, 1999.
-- The queue ADT: its signature is given in comments in the module
-- header.
module QueueState
( QueueState ,
addMessage, -- Inmess -> QueueState -> QueueState
queueStep, -- QueueState -> ( QueueState , [Outmess] )
queueStart, -- QueueState
queueLength, -- QueueState -> Int
queueEmpty -- QueueState -> Bool
) where
import Base -- for the base types of the system
type Time = Int
-- The implementation of the QueueState, where the first field gives the
-- current time, the second the service time so far for the item currently
-- being processed,
data QueueState = QS Time Service [Inmess]
deriving (Eq, Show)
-- To add a message, it is put at the end of the list of messages.
addMessage :: Inmess -> QueueState -> QueueState
addMessage im (QS time serv ml) = QS time serv (ml++[im])
-- A single step in the queue simulation.
queueStep :: QueueState -> ( QueueState , [Outmess] )
queueStep (QS time servSoFar (Yes arr serv : inRest))
| servSoFar < serv
= (QS (time+1) (servSoFar+1) (Yes arr serv : inRest) , [])
| otherwise
= (QS (time+1) 0 inRest , [Discharge arr (time-serv-arr) serv])
--
queueStep (QS time serv []) = (QS (time+1) serv [] , [])
-- The starting state
queueStart :: QueueState
queueStart = QS 0 0 []
-- The length of the queue
queueLength :: QueueState -> Int
queueLength (QS _ _ q) = length q
-- Is the queue empty?
queueEmpty :: QueueState -> Bool
queueEmpty (QS _ _ q) = (q==[])