Re: [Haskell-cafe] Automatic file closing after readFile

2007-10-21 Thread Albert Y. C. Lai
Magnus Therning wrote: I'll certainly try to look into all of that. However, I suspect your suggestion doesn't scale very well. On my original code it's easy, it was less than 10 lines, but how do I know where to start looking if it's a program of 100 lines, or 1000 lines? The problem could

Re: [Haskell-cafe] Automatic file closing after readFile

2007-10-21 Thread Duncan Coutts
On Sun, 2007-10-21 at 17:15 -0400, Albert Y. C. Lai wrote: Magnus Therning wrote: I'll certainly try to look into all of that. However, I suspect your suggestion doesn't scale very well. On my original code it's easy, it was less than 10 lines, but how do I know where to start looking if

Re: [Haskell-cafe] Automatic file closing after readFile

2007-10-19 Thread Magnus Therning
On Fri, Oct 19, 2007 at 02:09:01 +1000, Matthew Brecknell wrote: Magnus Therning: Just out of curiosity, how would I go about finding this myself? (Ideally it'd be an answer other than read the source for the libraries you are using. :-) Well, I can at least try to expand a little on read the

Re: [Haskell-cafe] Automatic file closing after readFile

2007-10-19 Thread Jules Bean
I agree with Matthew's comments in the post immediately before this. It takes him two decent paragraphs to explain what is going on, including a description of WHNF, a suggestion to use pen paper, a suggestion to read up on the semantics of unsafeInterleaveIO and more. What I find

Re: [Haskell-cafe] Automatic file closing after readFile

2007-10-18 Thread Matthew Brecknell
Magnus Therning: hasEmpty s = let _first_empty = s !! 0 == '\n' _last_empty = (reverse s) !! 1 == '\n' in _first_empty || _last_empty loadAndCheck fp = liftM hasEmpty $ readFile fp main = getArgs = filterM loadAndCheck = mapM_ putStrLn The one

Re: [Haskell-cafe] Automatic file closing after readFile

2007-10-18 Thread Jules Bean
Is there some (easy) way to avoid this while still using readFile? readFile' f = do s - readFile f return (length s `seq` s) (and curse the fact that the default readFile is unsafelazy). Jules ___ Haskell-Cafe mailing list

Re: [Haskell-cafe] Automatic file closing after readFile

2007-10-18 Thread Magnus Therning
On Thu, Oct 18, 2007 at 12:05:40 +0100, Jules Bean wrote: Is there some (easy) way to avoid this while still using readFile? readFile' f = do s - readFile f return (length s `seq` s) (and curse the fact that the default readFile is unsafelazy). :( Doesn't work. I'm starting to

Re: [Haskell-cafe] Automatic file closing after readFile

2007-10-18 Thread Magnus Therning
On Thu, Oct 18, 2007 at 20:58:45 +1000, Matthew Brecknell wrote: Magnus Therning: hasEmpty s = let _first_empty = s !! 0 == '\n' _last_empty = (reverse s) !! 1 == '\n' in _first_empty || _last_empty loadAndCheck fp = liftM hasEmpty $ readFile fp

Re: [Haskell-cafe] Automatic file closing after readFile

2007-10-18 Thread Matthew Brecknell
Magnus Therning: Still no cigar :( Yes, this is a little more subtle than I first thought. Look at liftM and filterM: liftM f m1 = do { x1 - m1; return (f x1) } filterM :: (Monad m) = (a - m Bool) - [a] - m [a] filterM _ [] = return [] filterM p (x:xs) = do flg - p x ys - filterM p xs

Re: [Haskell-cafe] Automatic file closing after readFile

2007-10-18 Thread Jules Bean
Magnus Therning wrote: On Thu, Oct 18, 2007 at 12:05:40 +0100, Jules Bean wrote: Is there some (easy) way to avoid this while still using readFile? readFile' f = do s - readFile f return (length s `seq` s) (and curse the fact that the default readFile is unsafelazy). :(

Re: [Haskell-cafe] Automatic file closing after readFile

2007-10-18 Thread David Roundy
On Thu, Oct 18, 2007 at 01:16:37PM +0100, Magnus Therning wrote: On Thu, Oct 18, 2007 at 20:58:45 +1000, Matthew Brecknell wrote: For a less hackish solution, you need to do a bit more work. Again, this is untested. loadAndCheck fn = bracket (openFile fn ReadMode) hClose checkContents

Re: [Haskell-cafe] Automatic file closing after readFile

2007-10-18 Thread Magnus Therning
On Thu, Oct 18, 2007 at 09:25:32 -0400, David Roundy wrote: On Thu, Oct 18, 2007 at 01:16:37PM +0100, Magnus Therning wrote: On Thu, Oct 18, 2007 at 20:58:45 +1000, Matthew Brecknell wrote: For a less hackish solution, you need to do a bit more work. Again, this is untested. loadAndCheck fn

Re: [Haskell-cafe] Automatic file closing after readFile

2007-10-18 Thread Magnus Therning
On Thu, Oct 18, 2007 at 22:58:48 +1000, Matthew Brecknell wrote: Magnus Therning: Still no cigar :( Yes, this is a little more subtle than I first thought. Look at liftM and filterM: liftM f m1 = do { x1 - m1; return (f x1) } filterM :: (Monad m) = (a - m Bool) - [a] - m [a] filterM _ [] =

Re: [Haskell-cafe] Automatic file closing after readFile

2007-10-18 Thread Magnus Therning
On Thu, Oct 18, 2007 at 22:58:48 +1000, Matthew Brecknell wrote: Magnus Therning: Still no cigar :( Yes, this is a little more subtle than I first thought. Look at liftM and filterM: liftM f m1 = do { x1 - m1; return (f x1) } filterM :: (Monad m) = (a - m Bool) - [a] - m [a] filterM _ [] =

Re: [Haskell-cafe] Automatic file closing after readFile

2007-10-18 Thread Matthew Brecknell
Magnus Therning: Just out of curiosity, how would I go about finding this myself? (Ideally it'd be an answer other than read the source for the libraries you are using. :-) Well, I can at least try to expand a little on read the source. :-) You'll first need a solid understanding of lazy

Re: [Haskell-cafe] Automatic file closing after readFile

2007-10-18 Thread Don Stewart
magnus: On Thu, Oct 18, 2007 at 12:05:40 +0100, Jules Bean wrote: Is there some (easy) way to avoid this while still using readFile? readFile' f = do s - readFile f return (length s `seq` s) (and curse the fact that the default readFile is unsafelazy). :( Doesn't work.