[Haskell-beginners] Trying to use map

Michael Litchard michael at schmong.org
Wed Jan 26 23:51:18 CET 2011


Here's what I'm working with, followed by what I am trying to do, and the
type error I get. I'm leaving things out that I do not think are important.
Let me know if I'm missing nessecary info.

> curlResp
>   :: (MonadError String m, MonadIO m)
>   => Curl -> URLString -> [CurlOption] -> m String --CurlResponse
> curlResp curl url opts = do
>   resp <- liftIO $ (do_curl_ curl url opts :: IO CurlResponse)
>   let code   = respCurlCode resp
>       status = respStatus resp
>   if code /= CurlOK || status /= 200
>      then throwError $ "Error: " ++ show code ++ " -- " ++ show status
>      else return $ respBody resp

> screenScraping :: String -> [String]
> screenScraping responseBody =
>                let collectedStrings = processHTML responseBody
>                    collectedIDLists = createIDList collectedStrings
>                    in constructedResourceURIs urlBase collectedIDLists

> resourceOpts :: [CurlOption]
> resourceOpts =
>   [ CurlHttpHeaders
>     [ "Accept: text/javascript, text/html, application/xml, text/xml, */*"
>     , "Accept-Language: en-us,en;q=0.5"
>     , "Accept-Charset:  ISO-8859-1,utf-8;q=0.7,*;q=0.7"
>     , "Keep-Alive:      115"
>     , "Connection:      keep-alive"
>     , "X-Requested-With:        XMLHttpRequest"
>     , "X-Prototype-Version:     1.6.0.3"
>     ]
>     , CurlEncoding "gzip,deflate"
>   ]



> obtainCookies :: IO Curl -> String -> IO ()
> obtainCookies curl responseBody = do
>               curl' <- curl
>               let collectedResources = screenScraping responseBody
>                   in mapM ( curlResp curl' resourceOpts)
collectedResources
>               return ()

> main :: IO ()
> main = do
>   curl <- initCurl
>   user:pass:_ <- getArgs
>   resp <- generateResourceHtml user pass
>
>   case resp of
>     Left err -> print err
>     Right body -> obtainCookies curl body

Here's the error I get.

    Couldn't match expected type `Char'
           against inferred type `CurlOption'
      Expected type: URLString
      Inferred type: [CurlOption]
    In the second argument of `curlResp', namely `resourceOpts'
    In the first argument of `mapM', namely
        `(curlResp curl' resourceOpts)'


The problem I see is a misformed mapM.

I am trying to do something like this


curlResp curl' resourceOpts "-here-be-a-url"

where collectedResources is a [String]. Not sure how to map over it
correctly.

The other problem I see is this
> obtainCookies :: IO Curl -> String -> IO ()
> obtainCookies curl responseBody = do
>               curl' <- curl
>               let collectedResources = screenScraping responseBody
>                   in mapM ( curlResp curl' resourceOpts)
collectedResources
>               return ()

this function does a monadic action (all I want is the cookies) and I don't
need the return value. I am not sure that the final line return (), is what
I want.


My primary question is this. how do I map over collectedResources correctly?
Secondary question, is the return correct?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110126/eba919f5/attachment.htm>


More information about the Beginners mailing list