简体   繁体   English

了解Haskell中的错误

[英]Understanding error in haskell

I (New to Haskell) am trying to perform unpack operation on ByteString that I receive from webpage. 我(是Haskell的新手)正尝试对从网页收到的ByteString进行解压缩操作。 Basically I want to search few words from webpage so I am trying to tokenize stream, then search word from words. 基本上,我想从网页中搜索几个单词,所以我试图标记流,然后从单词中搜索单词。

Prelude Network.HTTP.Conduit LB> LB.unpack (simpleHttp WebLink)

But I am getting below error 但是我正在错误以下

<interactive>:75:12: error:
• Couldn't match expected type ‘LB.ByteString’
              with actual type ‘m0 LB.ByteString’
• In the first argument of ‘LB.unpack’, namely...

From hackage I can see that its signature is 从黑客中我可以看到它的签名是

unpack :: ByteString -> [Word8] Source
O(n) Converts a ByteString to a '[Word8]'.

simpleHttp "http://example.com" is of type m ByteString , for some monad m , so for example of type IO ByteString . 对于某些monad msimpleHttp "http://example.com"的类型为m ByteString ,例如,类型为IO ByteString Using do notation you can get at the result. 使用do表示法可以得到结果。

import Network.HTTP.Conduit
import qualified Data.ByteString.Lazy.Char8 as LB

main :: IO ()
main = do
  res <- simpleHttp "http://example.com"
  let string = LB.unpack res
  putStr string

Or in ghci, 或者在ghci中

ghci> res <- simpleHttp "http://example.com"
ghci> LB.unpack res

simpleHttp WebLink appears to be a monadic action that returns a value, it's not a ByteString itself. simpleHttp WebLink似乎是一个返回值的单子动作,它本身不是ByteString。 You must run the procedure, obtaining the value, then (assuming it is a bytestring) you can unpack it. 您必须运行该过程,获取值,然后(假设它是一个字节串)可以将其解压缩。

Note that the simpleHttp procedure I know of does not return a bytestring. 请注意,我知道的simpleHttp过程不会返回字节simpleHttp You'll want to pattern match on the return value to inspect the Either type, if it is a response message instead of a failure then you can further pattern match on the response. 您需要对返回值进行模式匹配以检查Either类型,如果它是响应消息而不是失败,则可以进一步对响应进行模式匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM