haskell3.hs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. module Haskell3 where
  2. import qualified Data.Map as M
  3. import Control.Monad
  4. -- define Monads yeahhhh
  5. foo :: Maybe String
  6. foo = do
  7. x <- Just 3
  8. y <- Just "!"
  9. Just (show x ++ y)
  10. -- Carrier example with maps and monads double yeahhh
  11. type PersonName = String
  12. type PhoneNumber = String
  13. type BillingAddress = String
  14. data MobileCarrier = TIM | Vodadone | Wind deriving (Eq, Ord, Show)
  15. findCarrierBillingAddress :: PersonName
  16. -> M.Map PersonName Phonenumber
  17. -> M.Map PhoneNumber MobileCarrier
  18. -> M.Map MobileCarrier BillingAddress
  19. -> Maybe BillingAddress
  20. findCarrierBillingAddress person phoneMap carrierMap addressMap =
  21. case M.lookup person phoneMap of
  22. Nothing -> Nothing
  23. Just number ->
  24. case M.lookup number carrierMap of
  25. Nothing -> Nothing
  26. Just carrier -> M.lookup carrier addresMap
  27. findCarrierBillingAddress2 :: PersonName
  28. -> M.Map PersonName Phonenumber
  29. -> M.Map PhoneNumber MobileCarrier
  30. -> M.Map MobileCarrier BillingAddress
  31. -> Maybe BillingAddress
  32. findCarrierBillingAddress2 person phoneMap carrierMap addressMap = do
  33. number <- M.lookup person phoneMap
  34. carrier <- M.lookup number carrierMap
  35. M.lookup carrier addressMap