ソースを参照

some work on maps and monads

Andrea Gus 9 年 前
コミット
705f639e1a
1 ファイル変更44 行追加0 行削除
  1. 44 0
      haskell/haskell3.hs

+ 44 - 0
haskell/haskell3.hs

@@ -0,0 +1,44 @@
+
+module Haskell3 where
+
+import qualified Data.Map as M
+import Control.Monad
+
+-- define Monads yeahhhh
+
+foo :: Maybe String
+foo = do
+        x <- Just 3
+        y <- Just "!"
+        Just (show x ++ y)
+
+-- Carrier example with maps and monads double yeahhh
+
+type PersonName = String
+type PhoneNumber = String
+type BillingAddress = String
+data MobileCarrier = TIM | Vodadone | Wind deriving (Eq, Ord, Show)
+
+findCarrierBillingAddress :: PersonName 
+                         -> M.Map PersonName Phonenumber
+                         -> M.Map PhoneNumber MobileCarrier
+                         -> M.Map MobileCarrier BillingAddress 
+                         -> Maybe BillingAddress
+findCarrierBillingAddress person phoneMap carrierMap addressMap =
+        case M.lookup person phoneMap of
+          Nothing -> Nothing
+          Just number ->
+                  case M.lookup number carrierMap of
+                    Nothing -> Nothing
+                    Just carrier -> M.lookup carrier addresMap
+
+ 
+findCarrierBillingAddress2 :: PersonName 
+                         -> M.Map PersonName Phonenumber
+                         -> M.Map PhoneNumber MobileCarrier
+                         -> M.Map MobileCarrier BillingAddress 
+                         -> Maybe BillingAddress
+findCarrierBillingAddress2 person phoneMap carrierMap addressMap = do
+        number <- M.lookup person phoneMap
+        carrier <- M.lookup number carrierMap
+        M.lookup carrier addressMap