簡體   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