Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Bead9 where
- isJust :: Maybe a -> Bool
- isJust (Just _) = True
- isJust_ = False
- fromJust :: Maybe a -> a
- fromJust (Just x) = x
- catMaybes :: [Maybe a] -> [a]
- catMaybes [] = []
- catMaybes (x:xs)
- | isJust x = (fromJust x):catMaybes xs
- | otherwise = catMaybes xs
- mapMaybe :: (a -> Maybe b) -> [a] -> [b]
- mapMaybe f = catMaybes . map f
- safeHead :: [a] -> Maybe a
- safeHead[] = Nothing
- safeHead (x:xs) = Just x
- type Username = String
- type Password = String
- data Privilege = Simple | Admin
- deriving (Eq, Show)
- data Cookie = LoggedOut | LoggedIn Username Privilege
- deriving (Eq, Show)
- data Entry = Entry Password Privilege [Username]
- deriving (Eq, Show)
- type Database = [(Username, Entry)]
- richard, charlie, carol, david, kate :: (Username, Entry)
- richard = ("Richard", Entry "password1" Admin ["Kate"])
- charlie = ("Charlie", Entry "password2" Simple ["Carol"])
- carol = ("Carol", Entry "password3" Simple ["David", "Charlie"])
- david = ("David", Entry "password4" Simple ["Carol"])
- kate = ("Kate", Entry "password5" Simple ["Richard"])
- testDB :: Database
- testDB = [ richard, charlie, carol, david, kate ]
- testDBWithoutCarol :: Database
- testDBWithoutCarol =
- [ ("Richard", Entry "password1" Admin ["Kate"])
- , ("Charlie", Entry "password2" Simple [])
- , ("David", Entry "password4" Simple [])
- , ("Kate", Entry "password5" Simple ["Richard"])
- ]
- password :: Entry -> Password
- password (Entry pass k l) = pass
- privilege :: Entry -> Privilege
- privilege (Entry k x l) = x
- friends :: Entry -> [Username]
- friends (Entry k x l) = l
- mkCookie :: Username -> Password -> Entry -> Cookie
- mkCookie user pass (Entry pa pri t)
- | pass == pa = LoggedIn user pri
- | otherwise = LoggedOut
- login :: Username -> Password -> Database -> Cookie
- login user pas [(us, db)] = toLogin $ lookup(user, us) db where
- toLogin :: Maybe Entry -> Cookie
- toLogin Nothing = LoggedOut
- toLogin (Just p) = mkCookie user ps db
- --login user pass db =toCookie $ lookup(user,pass) db where
- --toCookie :: Maybe Privilege -> Cookie
- --toCookie Nothing = LoggedOut
- --toCookie (Just p) = LoggedIn user p
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement