Ok, I'm trying to wrap my head around IO in Haskell, and I figured I'd write a short little app dealing with web pages to do it. The snippet I'm getting tripped up at is (with apologies to bobince, though to be fair, I'm not trying to parse HTML here, just extract one or two values):
titleFromUrl url = do
(_, page) <- curlGetString url [CurlTimeout 60]
matchRegex (mkRegexWithOpts "<title>(.*?)</title>" False True) page
The above should take a URL in string form, scan the page it points to with matchRegex
, and return either Nothing
or Just [a]
, where a
is the matched (possibly multi-line) string. The frustrating thing is that when I try doing
Prelude> (_, page) <- curlGetString url [CurlTimeout 60]
Prelude> matchRegex (mkRegexWithOpts "<title>(.*?)</title>" False True) page
in the interpreter, it does precisely what I want it to. When I try to load the same expression, and associated imports
from a file, it gives me a type inference error stating that it couldn't match expected type 'IO b' against inferred type 'Maybe [String]'
. This tells me I'm missing something small and fundamental, but I can't figure out what. I've tried explicitly casting page
to a string, but that's just programming by superstition (and it didn't work in any case).
Any hints?