0

我正在编写简单的 sitemap.xml 爬虫。代码如下。我的问题是为什么最后的代码main不打印任何东西。我怀疑这是因为haskell的懒惰,但不知道如何处理它:

import Network.HTTP.Conduit
import qualified Data.ByteString.Lazy as L
import Text.XML.Light
import Control.Monad.Trans (liftIO)
import Control.Monad
import Data.String.Utils
import Control.Exception

download :: Manager -> Request -> IO (Either HttpException L.ByteString)
download manager req = do
  try $
    fmap responseBody (httpLbs req manager)

downloadUrl :: Manager -> String -> IO (Either HttpException L.ByteString)
downloadUrl manager url = do
  request <- parseUrl url
  download manager request

getPages :: Manager -> [String] -> IO [Either HttpException L.ByteString]
getPages manager urls =
  sequence $ map (downloadUrl manager) urls

main = withManager $ \ manager -> do
  -- I know simpleHttp is bad here
  mapSource <- liftIO $ simpleHttp "http://example.com/sitemap.xml"

  let elements = (parseXMLDoc mapSource) >>= Just . findElements (mapElement "loc")
      Just urls = liftM (map $ (replace "/#!" "?_escaped_fragment_=") . strContent) elements
      mapElement name = QName name (Just "http://www.sitemaps.org/schemas/sitemap/0.9") Nothing

  return $
    getPages manager urls >>= \ pages -> do
      print "evaluate me!"
      sequence $ map print pages
4

2 回答 2

2

你遇到了我在这里描述的同样的问题,至少就当它实际上应该给出类型错误时有错误的代码进行类型检查:为什么类型是“Main.main”,“IO()”而不是“ IO一“?. 这就是为什么您应该始终明确给出main类型签名的原因。main :: IO ()

要解决此问题,您需要替换returnlift(请参阅http://hackage.haskell.org/package/transformers/docs/Control-Monad-Trans-Class.html#v:lift)并替换sequence $ map ...mapM_. mapM_ f相当于sequence_ . map f

于 2014-04-08T00:05:06.930 回答
2

returnrunResourceThttp://hackage.haskell.org/package/resourcet-1.1.1/docs/Control-Monad-Trans-Resource.html#v:runResourceT )替换你的最后一个。正如它的类型所暗示的那样,它将 ResourceT 变成 IO 动作。

于 2014-04-07T13:35:47.870 回答