RE: closing file handles
Tom Pledger wrote:
[...] go' f = do h <- openFile f ReadMode text <- hGetContents h case text of (c:_) -> do hClose h return c
This way, the pattern match on `text' happens before the innermost `do', which in turn closes the handle before letting the rest of the computation continue.
I think the above code will lead to problems if file input is done when c is evaluated and not earlier.
Fortunately, in GHC this isn't the case. Evaluating the (:) is enough to force the character to be read. But to be on the safe side you could also write it like this: go' f = do h <- openFile f ReadMode text <- hGetContents h let c = head text c `seq` do { hClose h; return c } Cheers, Simon
Simon Marlow wrote:
go' f = do h <- openFile f ReadMode text <- hGetContents h let c = head text c `seq` do { hClose h; return c }
This doesn't compile for me. It complains that the last statement in a 'do' construct must be an expression...what gives? -- Hal Daume III "Computer science is no more about computers | hdaume@isi.edu than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume
At 9:44 am -0700 10/9/2001, Hal Daume wrote:
Simon Marlow wrote:
go' f = do h <- openFile f ReadMode text <- hGetContents h let c = head text c `seq` do { hClose h; return c }
This doesn't compile for me. It complains that the last statement in a 'do' construct must be an expression...what gives?
There's a layout error - the statements in the do should be left-aligned: go' f = do h <- openFile f ReadMode text <- hGetContents h let c = head text c `seq` do { hClose h; return c } It's probably caused by different systems having different tab-settings. (This may be stricter than the actual requirement, but there's no harm in that...) -- Dr. Lyndon While -------- Email - lyndon@cs.uwa.edu.au ,-_|\ Phone - +61 8 9380 2720 / \ Fax - +61 8 9380 1089 *_,-._/ Web - http://www.cs.uwa.edu.au/~lyndon v
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Tuesday 11 September 2001 04:06 am, you wrote:
There's a layout error - the statements in the do should be left-aligned:
It's probably caused by different systems having different tab-settings.
Hmmm. So, as suspected this layout works well as long as you use one editor and one system. I'm removing indentation-based syntax from my future language designs :) - -- Eray Ozkural (exa) <erayo@cs.bilkent.edu.tr> Comp. Sci. Dept., Bilkent University, Ankara www: http://www.cs.bilkent.edu.tr/~erayo GPG public key fingerprint: 360C 852F 88B0 A745 F31B EA0F 7C07 AE16 874D 539C -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQE7oh0hfAeuFodNU5wRAmSoAJ9QFP0vYjkdYzCBcG2DQlQ/aJKhjACfUN0q 92nDRB9pedlQVhwpg1/wXxQ= =5Pu7 -----END PGP SIGNATURE-----
participants (4)
-
Eray Ozkural -
Hal Daume -
Lyndon While -
Simon Marlow