[Haskell-beginners] calling inpure functions from pure code
Emmanuel Touzery
etouzery at gmail.com
Fri Oct 12 12:47:31 CEST 2012
Hello,
I'm trying to write my first real program in Haskell, a web page scraper;
at the top level, which is impure anyway, i fetch the top-level pages
(which is IO), then call the pure functions parsing the structure.
So far so good. That was the first part of the program and up to this
point I'm pretty sure I did it right (for the structure anyway). Most of
the demo programs and Haskell books cover this: you do the IO at the top
level, then you process and you come back to the top level to print your
results for instance.
But here comes the problem: these pure functions that parse the structure,
sometimes they find links and must open another page on the site... And
opening that new page, well it's IO and can't be pure.. Now if I call from
those pure methods IO methods, then they're not pure anymore, and in fact
since those are leaf calls basically my entire program becomes impure...
I had this idea, that I would make some sort of input data structure,
which would be like a lazy String reading from a file: doing IO while the
caller doesn't even realize and the caller can be pure. So some sort of
fake webserver or website or htmlpage data structure... which is lazy. And
then I give this data structure to my pure methods which parse the data,
they call functions on that structure, and they can stay pure. But it
sounds really contrived and probably completely the wrong solution.
I can see that this is a very basic question which was probably answered
hundreds of time, but I could not find the answer so far...
Thank you!
Emmanuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121012/df5417df/attachment.htm>
More information about the Beginners
mailing list