[Haskell-cafe] Simple network client

Judah Jacobson judah.jacobson at gmail.com
Wed Jan 30 12:51:14 EST 2008


On Jan 30, 2008 8:31 AM, Bryan O'Sullivan <bos at serpentine.com> wrote:
> Peter Verswyvelen wrote:
>
> > Then I tried the "seq" hack to force the handle opened by readFile to be closed, but that did not seem to work either. For example, the following still gave access denied:
> >
> > main = do
> >   cs <- readFile "L:/Foo.txt"
> >   writeFile "L:/Foo.txt" $ seq (length cs) cs
>
> This is unfortunately a classic beginner's mistake.  You got the seq
> wrong here, which is very common.
> [...]
> You need to float the call to seq out so that it's evaluated before the
> call to writeFile:
>
>   length cs `seq` writeFile cs
>

Another way of doing things: I've recently become a fan of
Control.Exception.evaluate:

main = do
  cs <- readFile "L:/Foo.txt"
  evalute (length cs)
  writeFile "L:/Foo.txt" cs

This might be easier for beginners to understand than messing around
with seq's (as long as you're already in the IO monad).

-Judah


More information about the Haskell-Cafe mailing list