Haskell Curl Help -
ok, i'm trying wrap head around io in haskell, , figured i'd write short little app dealing web pages it. snippet i'm getting tripped @ (with apologies bobince, though fair, i'm not trying parse html here, extract 1 or 2 values):
titlefromurl url = (_, page) <- curlgetstring url [curltimeout 60] matchregex (mkregexwithopts "<title>(.*?)</title>" false true) page
the above should take url in string form, scan page points matchregex
, , return either nothing
or just [a]
, a
matched (possibly multi-line) string. frustrating thing when try doing
prelude> (_, page) <- curlgetstring url [curltimeout 60] prelude> matchregex (mkregexwithopts "<title>(.*?)</title>" false true) page
in interpreter, precisely want to. when try load same expression, , associated imports
file, gives me type inference error stating couldn't match expected type 'io b' against inferred type 'maybe [string]'
. tells me i'm missing small , fundamental, can't figure out what. i've tried explicitly casting page
string, that's programming superstition (and didn't work in case).
any hints?
yeah, ghci accepts sort of value. can say:
ghci> 4 4 ghci> print 4 4
but 2 values (4
, print 4
) not equal. magic ghc doing if typed evaluates io something
executes action (and prints result if something
not ()
). if doesn't, calls show
on value , prints that. anyway, magic not accessible program.
when say:
do foo <- bar :: io int baz
baz
expected of type io something
, , it's type error otherwise. let execute i/o , return pure value. can check noting desugaring above yields:
bar >>= (\foo -> baz)
and
-- (specializing io simplicity) (>>=) :: io -> (a -> io b) -> io b
therefore
bar :: io foo :: baz :: io b
the way fix turn return value io value using return
function:
return :: -> io -- (again specialized io)
your code then:
titlefromurl url = (_, page) <- curlgetstring url [curltimeout 60] return $ matchregex (mkregexwithopts "<title>(.*?)</title>" false true) page
for of discussion above, can substitute monad io
(eg. maybe
, []
, ...) , still true.
Comments
Post a Comment