Question

I'm trying to rewrite a small program that scrapes information from one or more web pages and then turns it into flash cards. Little snippet here:

-- | this takes a string and produces IO [Tag String]
getVerbePage x = fmap parseTags $ openURL $ "http://leconjugueur.lefigaro.fr/conjugaison/verbe/" ++ x ++ ".html"

main = do
    tags <- getVerbePage "aller"
    -- | from here I do whatever I like with a clean [Tag String]

Doing one at a time I have no problem, the IO goes to the do loop and then I do what I need to with pure functions. I don't really understand how to do this in recurring fashion, basically what I'm after is:

-- | this takes a string and produces IO [Tag String]
getVerbePage x = fmap parseTags $ openURL $ "http://leconjugueur.lefigaro.fr/conjugaison/verbe/" ++ x ++ ".html"

main = do
    verbsString <- getLine -- | example input "aller pouvoir"
    let verbs = splitOn " " verbsString -- | list of strings
    pages <- getVerbePages verbs
    -- | from here use pure functions on pages, which would be type [[Tag String]]

getVerbePages :: [String] -> [[Tag String]] -- | I guess.
getVerbePages ps = ??????

The question is how do I write getVerbePages to loop over each string in ps and return it cleanly? I've been able to do alright with recursive actions and all that up to this point, very much a Haskell newb, but I don't understand how all this works when recurring an IO action.

Was it helpful?

Solution

If you want to repeat the same IO action over a list of things, then you can use mapM. Its type signature is (specialized here to IO)

mapM :: (a -> IO b) -> [a] -> IO [b]

Using it with your getVerbPage would mean that the a type variable is String and the b type variable is [Tag String]. Then you would have mapM getVerbPage :: [String] -> IO [[Tag String]], which is what you want as the definition of getVerbPages.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top