我是 Haskell 的新手,我只是按照 RWH 上的示例进行操作。我在第 14 章的以下程序中遇到问题:
import qualified Data.Map as M
type PersonName = String
type PhoneNumber = String
type BillingAddress = String
data MobileCarrier = Honest_Bobs_Phone_Network
| Morrisas_Marvelous_Mobiles
| Petes_Plutocratic_Phones
deriving (Eq, Ord)
findCarrierBillingAddress :: PersonName
-> M.Map PersonName PhoneNumber
-> M.Map PhoneNumber MobileCarrier
-> M.Map MobileCarrier BillingAddress
-> Maybe BillingAddress
-- This will work
findCarrierBillingAddress person phoneMap carrierMap addressMap = do
phone <- M.lookup person phoneMap
carrier <- M.lookup phone carrierMap
address <- M.lookup carrier addressMap
return address
-- This will NOT work:
findCarrierBillingAddress person phoneMap carrierMap addressMap =
return person >>=
lookup phoneMap >>=
lookup carrierMap >>=
lookup addressMap
where lookup = flip M.lookup
似乎在使用 >>= 以 monad 链接格式编写 findCarrierBillingAddres 时,它只是不进行类型检查:
/home/bruce/Programming/haskell/real/ch14/hello.hs:21:9:
Couldn't match type `[Char]' with `MobileCarrier'
Expected type: MobileCarrier -> Maybe BillingAddress
Actual type: PersonName -> Maybe BillingAddress
In the return type of a call of `lookup'
In the second argument of `(>>=)', namely `lookup addressMap'
In the expression:
return person >>= lookup phoneMap >>= lookup carrierMap
>>= lookup addressMap
/home/bruce/Programming/haskell/real/ch14/hello.hs:21:16:
Couldn't match type `MobileCarrier' with `[Char]'
Expected type: M.Map PersonName BillingAddress
Actual type: M.Map MobileCarrier BillingAddress
In the first argument of `lookup', namely `addressMap'
In the second argument of `(>>=)', namely `lookup addressMap'
In the expression:
return person >>= lookup phoneMap >>= lookup carrierMap
>>= lookup addressMap
Failed, modules loaded: none.
问题是..为什么使用 >>= 的第二种格式不会进行类型检查?