Sho
Sho

Reputation: 25

Type errors when using IO action in Haskell

I just started learning Haskell and now got stuck in dealing with IO action.

Here's the code.

parseDnsMessage :: BG.BitGet DnsMessage

recQuery :: BS.ByteString -> String -> IO BS.ByteString

resolveName :: [Word8] -> [Word8] -> BS.ByteString -> String
resolveName qname name bstr = do
  let newbstr = BSL.toStrict $ replace (BS.pack qname) (BS.pack name) bstr
  retbstr <- recQuery newbstr (head rootServers4)
  let msg = BG.runBitGet retbstr parseDnsMessage
  case msg of
    Right m -> (intercalate "." $ map show (rdata $ head $ answer $ m))

---Error Message---

Couldn't match expected type ‘[BSI.ByteString]’
            with actual type ‘IO BSI.ByteString’
In a stmt of a 'do' block:
  retbstr <- recQuery newbstr (head rootServers4)
In the expression:
  do { let newbstr
             = BSL.toStrict $ replace (BS.pack qname) (BS.pack name) bstr;
       retbstr <- recQuery newbstr (head rootServers4);
       let msg = BG.runBitGet retbstr parseDnsMessage;
       case msg of {
         Right m
           -> (intercalate "." $ map show (rdata $ head $ answer $ m)) } }

I just want to retrieve BS.ByteString from the recQuery IO action.

How can I fix this?

Upvotes: 0

Views: 112

Answers (1)

Will Sewell
Will Sewell

Reputation: 2643

The problem is your resolveName should return IO String, not String. This is because it is operating in the IO monad, i.e. you are chaining IO actions in it, so it must return IO.

Upvotes: 1

Related Questions