0

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?

5
  • 1
    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. Commented Feb 29, 2016 at 14:46
  • 2
    Possible duplicate: convert IO String to “Other type” Commented Feb 29, 2016 at 14:52
  • 2
    Oh that makes sense! Thank you very much. Commented Feb 29, 2016 at 14:59
  • @WillSewell since your comment resolved the question, perhaps it should be posted as an answer? Commented Mar 2, 2016 at 4:59
  • @sclv ok I'll post it as an answer. Commented Mar 2, 2016 at 10:05

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.