了解 haskell 中的错误

Understanding error in haskell

我(Haskell 的新手)正在尝试对从网页收到的 ByteString 执行解包操作。基本上我想从网页中搜索几个词,所以我试图标记流,然后从词中搜索词。

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

但我遇到了以下错误

<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...

从 hackage 我可以看到它的签名是

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

simpleHttp WebLink 似乎是 returns 一个值的单子动作,它本身不是 ByteString。你必须运行程序,获取值,然后(假设它是一个bytestring)你可以解压它。

请注意,我所知道的 simpleHttp 过程不是 return 字节串。您需要对 return 值进行模式匹配以检查 Either 类型,如果它是响应消息而不是失败,那么您可以进一步对响应进行模式匹配。

simpleHttp "http://example.com"m ByteString 类型,对于某些 monad m,例如 IO ByteString 类型。使用 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

或者在 ghci 中,

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