
Hi, One thing that seems to keep biting me is strictness in do blocks. What I want to know is the generic way to force an entire String (or other list, perhaps from hGetContents) to be evaluated (read into RAM, I guess) so the underlying file can be closed.... and do it right now. One quick hack that works is hPutStrLn on it, right where I want it to be evaluated. But ugly and not always appropriate. Passing it to some other IO-based functions also works, but again, that doesn't "feel" right. I can always stumble upon things that work, but I'm never sure if I've got 'em right. So, what is the idiomatic way to do this? Most of the documents I see out there on this topic are for usage outside the I/O monad, and I'm not seeing how they apply here. Thanks, -- John

What I want to know is the generic way to force an entire String (or other list, perhaps from hGetContents) to be evaluated (read into RAM, I guess) so the underlying file can be closed.... and do it right now. What I have done in the past is to take the length of the string, and test the length in some way - although I guess you could call seq on
Hi, the length. Of course, the length tests technically doesn't evaluate the character positions (just the spine of the list), but it always works in practice. Thanks Neil

On Wed, 2006-07-05 at 12:08 +0100, Neil Mitchell wrote:
Hi,
What I want to know is the generic way to force an entire String (or other list, perhaps from hGetContents) to be evaluated (read into RAM, I guess) so the underlying file can be closed.... and do it right now. What I have done in the past is to take the length of the string, and test the length in some way - although I guess you could call seq on the length.
evaluate is for just that purpose. evaluate (length input) from the docs: http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Exception.htm... evaluate :: a -> IO a Forces its argument to be evaluated, and returns the result in the IO monad. It can be used to order evaluation with respect to other IO operations; its semantics are given by evaluate undefined `seq` return () ==> return () catch (evaluate undefined) (\e -> return ()) ==> return () NOTE: (evaluate a) is not the same as (a `seq` return a). Duncan

On 2006-07-05, Duncan Coutts
What I have done in the past is to take the length of the string, and test the length in some way - although I guess you could call seq on the length.
evaluate is for just that purpose.
evaluate (length input)
from the docs:
http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Exception.htm...
That looks like what I need, but why is it in Control.Exception instead of System.IO? If it were in System.IO, I would have found it.

On 7/5/06, John Goerzen
On 2006-07-05, Duncan Coutts
wrote: evaluate is for just that purpose.
evaluate (length input)
from the docs:
http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Exception.htm...
That looks like what I need, but why is it in Control.Exception instead of System.IO? If it were in System.IO, I would have found it.
I suppose one of its main purposes is to catch the stuff thrown by undefined and error.

I can always stumble upon things that work, but I'm never sure if I've got 'em right. So, what is the idiomatic way to do this?
I think something like
evaluate (rnf myString)
Evaluate is an action that forces the value to be evaluated. rnf (from Control.Parallel.Strategies) is 'reduce to normal form', i.e. deepSeq. Twan

Hello John, Wednesday, July 5, 2006, 3:04:22 PM, you wrote:
What I want to know is the generic way to force an entire String (or other list, perhaps from hGetContents) to be evaluated (read into RAM, I guess) so the underlying file can be closed.... and do it right now.
eval [] = [] eval (x:xs) = eval xs do str <- getContents return $! eval str .... is my usual hack. there is also `deepSeq` if you need to fully evaluate more complex structure. btw, this 'eval' don't evaluate list elements - it should be not a problem for evaluation result of 'getContents' -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
participants (6)
-
Bulat Ziganshin
-
Duncan Coutts
-
ihope
-
John Goerzen
-
Neil Mitchell
-
Twan van Laarhoven