繁体   English   中英

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

从黑客中我可以看到它的签名是

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

对于某些monad msimpleHttp "http://example.com"的类型为m ByteString ,例如,类型为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

simpleHttp WebLink似乎是一个返回值的单子动作,它本身不是ByteString。 您必须运行该过程,获取值,然后(假设它是一个字节串)可以将其解压缩。

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

暂无
暂无

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

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