[Haskell-beginners] A first try
Manfred Lotz
manfred.lotz at arcor.de
Sun Jun 26 22:59:19 CEST 2011
On Sun, 26 Jun 2011 15:02:07 -0400
David Place <d at vidplace.com> wrote:
>
> On Jun 26, 2011, at 1:29 PM, Manfred Lotz wrote:
>
> > When I stumbled upon lazy IO newbie-wise I was pointed to withFile
> > resp. bracket by Daniel Fischer and now that I know how to do it it
> > seems fine to me. It also alerted me to pay more attention to
> > lazyness as this is a Haskell immanent thingie.
>
> Of course, it is possible to use hGetContents with withFile. You can
> still get into trouble because hGetContents is unsafe. Beginners get
> tripped up trying to do something like the following getting
> unexpected results. (i remember I did.)
>
> print10 =
> do
> contents <- withFile "/usr/share/dict/words" ReadMode (\h ->
> hGetContents h) print $ take 10 contents
>
> So, you have to do this keeping in mind a rather procedural model of
> the evaluation of the lazy data structures. I feel this is not very
> declarative or intuitive.
>
> print10' =
> do
> h <- openFile "/usr/share/dict/words" ReadMode
> contents <- hGetContents h
> print $ take 10 contents
> hClose h
>
> Iteratee IO provides a declarative way to do this that is safe,
> compositional and efficient. But not yet very pretty. In haskell
> cafe, John Lato says that he is working on that.
>
>
Thanks for showing this. I have to admit that I'm as a beginner not in
a position to judge about the merits of Iteratee IO versus the standard
way. Just wanted to point out that the way I work thru the XML files in
my particular task seems to work fine. I will watch what happens to
Iteratee IO.
Is there any problem in the code snippet I pasted? If so I would
like to get a hint of course.
--
Thanks,
Manfred
More information about the Beginners
mailing list