Skip to content

Instantly share code, notes, and snippets.

@monzou
Created June 17, 2012 03:44
Show Gist options
  • Save monzou/2943335 to your computer and use it in GitHub Desktop.
Save monzou/2943335 to your computer and use it in GitHub Desktop.
import qualified Data.Map as Map
data LockerState = Taken | Free deriving (Show, Eq)
type Code = String
type LockerMap = Map.Map Int (LockerState, Code)
lookupLocker :: Int -> LockerMap -> Either String Code
lookupLocker 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."
main =
let
lockers = Map.fromList
[
(100, (Taken, "A100")),
(101, (Taken, "A101")),
(102, (Free, "A102")),
(103, (Free, "A103"))
]
in
print $ lookupLocker 100 lockers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment