Advertisement
Guest User

Untitled

a guest
Dec 8th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Bead9 where
  2.  
  3. isJust :: Maybe a -> Bool
  4. isJust (Just _) = True
  5. isJust_ = False
  6.  
  7. fromJust :: Maybe a -> a
  8. fromJust (Just x) = x
  9.  
  10. catMaybes :: [Maybe a] -> [a]
  11. catMaybes [] = []
  12. catMaybes (x:xs)
  13.     | isJust x = (fromJust x):catMaybes xs
  14.     | otherwise = catMaybes xs
  15.  
  16. mapMaybe :: (a -> Maybe b) -> [a] -> [b]
  17. mapMaybe f =  catMaybes . map f
  18.  
  19. safeHead :: [a] -> Maybe a
  20. safeHead[] = Nothing
  21. safeHead (x:xs) = Just x
  22.  
  23. type Username = String
  24. type Password = String
  25.  
  26. data Privilege = Simple | Admin
  27.   deriving (Eq, Show)
  28.  
  29. data Cookie = LoggedOut | LoggedIn Username Privilege
  30.   deriving (Eq, Show)
  31.  
  32. data Entry = Entry Password Privilege [Username]
  33.   deriving (Eq, Show)
  34.  
  35. type Database = [(Username, Entry)]
  36.  
  37. richard, charlie, carol, david, kate :: (Username, Entry)
  38. richard = ("Richard", Entry "password1" Admin  ["Kate"])
  39. charlie = ("Charlie", Entry "password2" Simple ["Carol"])
  40. carol   = ("Carol",   Entry "password3" Simple ["David", "Charlie"])
  41. david   = ("David",   Entry "password4" Simple ["Carol"])
  42. kate    = ("Kate",    Entry "password5" Simple ["Richard"])
  43.  
  44. testDB :: Database
  45. testDB = [ richard, charlie, carol, david, kate ]
  46.  
  47. testDBWithoutCarol :: Database
  48. testDBWithoutCarol =
  49.   [ ("Richard", Entry "password1" Admin  ["Kate"])
  50.   , ("Charlie", Entry "password2" Simple [])
  51.   , ("David",   Entry "password4" Simple [])
  52.   , ("Kate",    Entry "password5" Simple ["Richard"])
  53.   ]
  54.  
  55. password :: Entry -> Password
  56. password (Entry pass k l) = pass
  57.  
  58. privilege :: Entry -> Privilege
  59. privilege (Entry k x l) = x
  60.  
  61. friends :: Entry -> [Username]
  62. friends (Entry k x l) = l
  63.  
  64. mkCookie :: Username -> Password -> Entry -> Cookie
  65. mkCookie user pass (Entry pa pri t)
  66.       | pass == pa = LoggedIn user pri
  67.       | otherwise = LoggedOut
  68.  
  69. login :: Username -> Password -> Database -> Cookie
  70. login user pas [(us, db)] = toLogin $ lookup(user, us) db where
  71.   toLogin :: Maybe Entry -> Cookie
  72.   toLogin Nothing = LoggedOut
  73.   toLogin (Just p) = mkCookie user ps db
  74.  
  75.  
  76. --login user pass db =toCookie $ lookup(user,pass) db where
  77.   --toCookie :: Maybe Privilege -> Cookie
  78.   --toCookie Nothing = LoggedOut
  79.   --toCookie (Just p) = LoggedIn user p
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement