这段代码不打打字机:
import Network.HTTP.Conduit
import qualified Data.ByteString.Char8 as BS
main :: IO ()
main = do
resp <- simpleHttp "http://www.google.com"
putStrLn $ BS.unpack resp引发以下错误:
Couldn't match expected type `BS.ByteString'
with actual type `Data.ByteString.Lazy.Internal.ByteString'
In the first argument of `BS.unpack', namely `resp'
In the second argument of `($)', namely `BS.unpack resp'
In a stmt of a 'do' block: putStrLn $ BS.unpack resp
Failed, modules loaded: none.怎么解决这个问题?更改为其他ByteString变体不起作用。
simpleHttp函数的类型如下:simpleHttp :: Control.Monad.IO.Class.MonadIO m => String -> m Data.ByteString.Lazy.Internal.ByteString。因此,我尝试在IO monad中获取ByteString并尝试对其进行unpack,但是这会导致一个错误。
发布于 2014-01-14 14:18:54
有两个独立的ByteString模块,一个用于惰性ByteStrings,另一个用于严格ByteStrings。simpleHTTP返回一个懒惰的字节串,但是您导入了严格的字节串模块,所以unpack需要一个严格的字节串。
试着改变
import qualified Data.ByteString.Char8 as BS至
import qualified Data.ByteString.Lazy.Char8 as BS也就是说,如果使用字节码模块的Char8版本,则需要小心,因为字符串<-> bytestring转换只有在使用ASCII编码时才能工作。我建议使用适当的编码函数的将字节串转换为文本,然后打印它。
https://stackoverflow.com/questions/21115462
复制相似问题