-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocker.hs
More file actions
28 lines (21 loc) · 863 Bytes
/
locker.hs
File metadata and controls
28 lines (21 loc) · 863 Bytes
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
import qualified Data.Map as Map
data LockerState = Taken | Free deriving (Show, Eq)
type Code = String
-- 定义了一种叫LockerMap的格式(k, (s, c)) k表示箱子的索引,s表示锁定状态,c表示密码
type LockerMap = Map.Map Int (LockerState, Code)
lockerLookup :: Int -> LockerMap -> Either String Code
lockerLookup lockerNumber map = case Map.lookup lockerNumber map of
Nothing -> Left $ "Locker" ++ show lockerNumber ++ " doesn't exist!"
Just (state, code) -> if state /= Taken
then Right code
else Left $ "Locker" ++ show lockerNumber ++ " is already taken!"
lockers :: LockerMap
lockers = Map.fromList
[
(100, (Taken, "ZD39I")),
(101, (Free, "JAH3I")),
(103, (Free, "OQSA9")),
(105, (Free, "IQTSA")),
(109, (Taken, "893JJ")),
(110, (Taken, "99292"))
]