Note that there are some explanatory texts on larger screens.

plurals
  1. POType check error in Haskell monad chaining
    primarykey
    data
    text
    <p>I'm new to Haskell and I'm just following the example on RWH. I'm having problems with the following programs on Chapter 14:</p> <pre><code>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 -&gt; M.Map PersonName PhoneNumber -&gt; M.Map PhoneNumber MobileCarrier -&gt; M.Map MobileCarrier BillingAddress -&gt; Maybe BillingAddress -- This will work findCarrierBillingAddress person phoneMap carrierMap addressMap = do phone &lt;- M.lookup person phoneMap carrier &lt;- M.lookup phone carrierMap address &lt;- M.lookup carrier addressMap return address -- This will NOT work: findCarrierBillingAddress person phoneMap carrierMap addressMap = return person &gt;&gt;= lookup phoneMap &gt;&gt;= lookup carrierMap &gt;&gt;= lookup addressMap where lookup = flip M.lookup </code></pre> <p>It seems that when writing findCarrierBillingAddres in the monad chaining format using >>=, it just does not type check:</p> <pre><code>/home/bruce/Programming/haskell/real/ch14/hello.hs:21:9: Couldn't match type `[Char]' with `MobileCarrier' Expected type: MobileCarrier -&gt; Maybe BillingAddress Actual type: PersonName -&gt; Maybe BillingAddress In the return type of a call of `lookup' In the second argument of `(&gt;&gt;=)', namely `lookup addressMap' In the expression: return person &gt;&gt;= lookup phoneMap &gt;&gt;= lookup carrierMap &gt;&gt;= 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 `(&gt;&gt;=)', namely `lookup addressMap' In the expression: return person &gt;&gt;= lookup phoneMap &gt;&gt;= lookup carrierMap &gt;&gt;= lookup addressMap Failed, modules loaded: none. </code></pre> <p>The question is.. why the second format using >>= would not type check? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload